<commit_before>import { UserInterface } from './../interfaces/user';
import { UserData } from './user-data.service';
import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
import * as firebase from 'firebase/app';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { EventEmitter } from 'events';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class AuthService {
    authState: any = null;
    public authUpdated: Subject<boolean> = new Subject<boolean>();

    constructor(
        private afAuth: AngularFireAuth,
        private db: AngularFireDatabase,
        private router: Router,
        private userData: UserData) {

        this.afAuth.authState.subscribe((auth) => {
            this.authState = auth;
            this.authUpdated.next(this.authState);
        });
    }

    get isAuthenticated(): boolean {
        return this.authState !== null;
    }

    get currentUser(): any {
        return this.isAuthenticated ? this.authState : null;
    }

    get currentUserObservable(): any {
        return this.afAuth.authState;
    }

    get currentUserId(): string {
        return this.isAuthenticated ? this.authState.uid : '';
    }

    get currentUserAnonymous(): boolean {
        return this.isAuthenticated ? this.authState.isAnonymous : false;
    }

    get currentUserDisplayName(): string {
        if (!this.authState) {
            return 'Guest';
        } else if (this.currentUserAnonymous) {
            return 'Anonymous';
        } else {
            return this.authState['displayName'] || 'User without a Name';
        }
    }

    emailSignUp(email: string, password: string, model: UserInterface) {
        return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
            .then((user) => {
                user.updateProfile({ displayName: `${model.firstName} ${model.lastName}` });
                this.authState = user;
            })
            .then(() => {
                this.userData.add(this.currentUserId, model);
                this.router.navigateByUrl('/');
            })
            .catch(error => console.log(error));
    }

    emailLogin(email: string, password: string) {
        return this.afAuth.auth.signInWithEmailAndPassword(email, password)
            .then((user) => {
                this.authState = user;
                this.router.navigateByUrl('/user/profile');
            })
            .catch(error => console.log(error));
    }

    resetPassword(email: string) {
        const fbAuth = firebase.auth();

        return fbAuth.sendPasswordResetEmail(email)
            .then(() => {
                console.log('email sent');
                this.signOut();
            })
            .catch((error) => console.log(error));
    }

    changeEmail(oldEmail: string, newEmail: string, password: string) {
        this.afAuth.auth.signInWithEmailAndPassword(oldEmail, password)
            .then((user) => {
                user.updateEmail(newEmail);
                this.userData.update(this.currentUserId, { email: newEmail });
            });

    }

    signOut(): void {
        this.afAuth.auth.signOut();
        this.router.navigate(['/']);
    }
}<commit_msg>uid and email added in localStorage<commit_after>import { UserInterface } from './../interfaces/user';
import { UserData } from './user-data.service';
import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { Router } from '@angular/router';
import { Subject } from 'rxjs/Subject';
import { LOCALSTORAGE_AUTH_KEY_NAME, LOCALSTORAGE_EMAIL_KEY_NAME } from '../common/constants';

@Injectable()
export class AuthService {
    authState: any = null;
    public authUpdated: Subject<boolean> = new Subject<boolean>();

    constructor(
        private afAuth: AngularFireAuth,
        private db: AngularFireDatabase,
        private router: Router,
        private userData: UserData) {

        this.afAuth.authState.subscribe((auth) => {
            this.authState = auth;
            this.authUpdated.next(this.authState);
        });
    }

    get isAuthenticated(): boolean {
        return this.authState !== null;
    }

    get currentUser(): any {
        return this.isAuthenticated ? this.authState : null;
    }

    get currentUserObservable(): any {
        return this.afAuth.authState;
    }

    get currentUserId(): string {
        return this.isAuthenticated ? this.authState.uid : '';
    }

    get currentUserAnonymous(): boolean {
        return this.isAuthenticated ? this.authState.isAnonymous : false;
    }

    get currentUserDisplayName(): string {
        if (!this.authState) {
            return 'Guest';
        } else if (this.currentUserAnonymous) {
            return 'Anonymous';
        } else {
            return this.authState['displayName'] || 'User without a Name';
        }
    }

    emailSignUp(email: string, password: string, model: UserInterface) {
        return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
            .then((user) => {
                user.updateProfile({ displayName: `${model.firstName} ${model.lastName}` });
                this.authState = user;
            })
            .then(() => {
                this.userData.add(this.currentUserId, model);
                this.router.navigateByUrl('/');
            })
            .catch(error => console.log(error));
    }

    emailLogin(email: string, password: string) {
        return this.afAuth.auth.signInWithEmailAndPassword(email, password)
            .then((user) => {
                this.authState = user;
                localStorage.setItem(LOCALSTORAGE_AUTH_KEY_NAME, user.uid);
                localStorage.setItem(LOCALSTORAGE_EMAIL_KEY_NAME, user.email);
                this.router.navigateByUrl('/user/profile');
            })
            .catch(error => console.log(error));
    }

    resetPassword(email: string) {
        const fbAuth = firebase.auth();

        return fbAuth.sendPasswordResetEmail(email)
            .then(() => {
                console.log('email sent');
                this.signOut();
            })
            .catch((error) => console.log(error));
    }

    changeEmail(oldEmail: string, newEmail: string, password: string) {
        this.afAuth.auth.signInWithEmailAndPassword(oldEmail, password)
            .then((user) => {
                user.updateEmail(newEmail);
                this.userData.update(this.currentUserId, { email: newEmail });
            });

    }

    signOut(): void {
        this.afAuth.auth.signOut()
            .then(() => {
                localStorage.removeItem(LOCALSTORAGE_AUTH_KEY_NAME);
                localStorage.removeItem(LOCALSTORAGE_EMAIL_KEY_NAME);
                this.router.navigate(['/']);
            });
    }
}
{'fraction_non_alphanumeric': 0.12522890547964502, 'fraction_numerical': 0.0005634596422031273, 'mean_word_length': 2.2494279176201375, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '797573', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1929, 'n_words': 380}