import { Injectable } from '@angular/core' ; import { AngularFireAuth } from 'angularfire2/auth' ; import { AngularFirestore , AngularFirestoreDocument } from 'angularfire2/firestore' ; import { Router } from '@angular/router' ; import { Observable } from 'rxjs/Observable' ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import 'rxjs/add/observable/of' ; import 'rxjs/add/operator/switchMap' ; import { User } from './user' ; import { DatabaseService } from '../database.service' ; @ Injectable ( ) export class AuthService { authState : any = null ; user : BehaviorSubject < User > = new BehaviorSubject ( null ) ; currentUID : string ; admin : boolean ; constructor ( private afAuth , private db , private afs , private router ) { this . afAuth . authState . switchMap ( auth => { if ( auth ) { return this . afs . doc < User > ( 'users/' + auth . uid ) . valueChanges ( ) ; } else { return Observable . of ( null ) ; } } ) . subscribe ( user => { this . user . next ( user ) ; } ) ; } signup ( email , password ) { this . afAuth . auth . createUserWithEmailAndPassword ( email , password ) . then ( userData => { const newUser = { email : email , cashier : true , admin : false } ; this . afs . collection ( 'users' ) . doc ( userData . uid ) . set ( newUser ) ; } ) ; } emailLogin ( email , password ) { return this . afAuth . auth . signInWithEmailAndPassword ( email , password ) . then ( ( user ) => { this . authState = user ; console . log ( 'Login success' ) ; this . currentUID = this . afAuth . auth . currentUser . uid ; this . checkAdmin ( this . currentUID ) ; this . router . navigate ( [ '/welcome' ] ) ; } ) ; } anonymousLogin ( ) { return this . afAuth . auth . signInAnonymously ( ) . then ( ( user ) => { this . authState = user ; console . log ( 'Login success' ) ; this . currentUID = 'guest' ; this . checkAdmin ( this . currentUID ) ; this . router . navigate ( [ '/welcome' ] ) ; } ) ; } resetPassword ( email ) { return this . afAuth . auth . sendPasswordResetEmail ( email ) . then ( ( ) => console . log ( 'email sent' ) ) ; } updateUserRole ( id , roles ) { this . db . updateUser ( id , roles ) ; } checkAdmin ( uid ) { if ( uid !== 'guest' ) { this . db . getUser ( uid ) . valueChanges ( ) . take ( 1 ) . subscribe ( data => this . admin = data . admin ) ; } else { this . admin = false ; } } signOut ( ) { this . afAuth . auth . signOut ( ) . then ( ( ) => { this . router . navigate ( [ '/' ] ) ; this . authState = null ; this . currentUID = null ; this . admin = false ; } ) ; console . log ( 'Logged out' ) ; } }