import { Inject , Injectable , PLATFORM_ID } from '@angular/core' ; import { FirebaseAuthService } from './../firebase-auth.service' ; import { AngularFireAuth } from '@angular/fire/auth' ; import { AngularFirestore } from '@angular/fire/firestore' ; import * as firebase from 'firebase/app' ; import { Router } from '@angular/router' ; import { MatDialog , MatDialogRef } from '@angular/material' ; import { LoginComponent } from './../../components/login/login.component' ; import { WindowRef } from './../../services/windowref.service' ; import { isPlatformBrowser } from '@angular/common' ; import { User , UserStatusConstants , CollectionConstants , TriggerConstants } from 'shared-library/shared/model' ; import { AngularFireDatabase } from '@angular/fire/database' ; @ Injectable ( ) export class WebFirebaseAuthService implements FirebaseAuthService { dialogRef : MatDialogRef < LoginComponent > ; private user : User ; private isUserOnline = false ; constructor ( protected afAuth , public router , protected afStore , public dialog , @ Inject ( PLATFORM_ID ) private platformId , private windowsRef , private db ) { } authState ( ) { return this . afAuth . authState ; } public createUserWithEmailAndPassword ( email , password ) { return this . afAuth . auth . createUserWithEmailAndPassword ( email , password ) ; } public getIdToken ( user , forceRefresh ) { if ( this . dialogRef ) { this . dialogRef . close ( ) ; } return user . getIdToken ( forceRefresh ) ; } public refreshToken ( forceRefresh ) { return this . getFirebaseUser ( ) . getIdToken ( forceRefresh ) ; } public async updatePassword ( email , currentPassword , newPassword ) { try { const credentials = firebase . auth . EmailAuthProvider . credential ( email , currentPassword ) ; await this . getFirebaseUser ( ) . reauthenticateWithCredential ( credentials ) ; await this . getFirebaseUser ( ) . updatePassword ( newPassword ) ; return 'success' ; } catch ( error ) { console . log ( 'error---->' , error ) ; throw error ; } } private getFirebaseUser ( ) { return firebase . auth ( ) . currentUser ; } public async signOut ( ) { this . updateTokenStatus ( this . user . userId , UserStatusConstants . OFFLINE ) ; await this . afAuth . auth . signOut ( ) ; this . router . navigate ( [ 'dashboard' ] ) ; if ( isPlatformBrowser ( this . platformId ) ) { this . windowsRef . nativeWindow . location . reload ( ) ; } } public showLogin ( ) { this . dialogRef = this . dialog . open ( LoginComponent , { disableClose : false } ) ; } public sendEmailVerification ( user ) { return firebase . auth ( ) . currentUser . sendEmailVerification ( ) ; } public signInWithEmailAndPassword ( email , password ) { return this . afAuth . auth . signInWithEmailAndPassword ( email , password ) ; } public firebaseAuth ( ) { return this . afAuth . auth ; } public googleLogin ( ) { return this . afAuth . auth . signInWithPopup ( new firebase . auth . GoogleAuthProvider ( ) ) ; } public facebookLogin ( ) { return this . afAuth . auth . signInWithPopup ( new firebase . auth . FacebookAuthProvider ( ) ) ; } public phoneLogin ( phoneNumber ) { return ; } public twitterLogin ( ) { return this . afAuth . auth . signInWithPopup ( new firebase . auth . TwitterAuthProvider ( ) ) ; } public githubLogin ( ) { return this . afAuth . auth . signInWithPopup ( new firebase . auth . GithubAuthProvider ( ) ) ; } public appleLogin ( ) { const auth = firebase . auth ( ) ; const provider = new firebase . auth . OAuthProvider ( 'apple.com' ) ; return auth . signInWithPopup ( provider ) ; } public sendPasswordResetEmail ( email ) { return firebase . auth ( ) . sendPasswordResetEmail ( email ) ; } public resumeState ( user ) { } public updatePushToken ( token ) { } public updateOnConnect ( user ) { this . user = user ; this . db . object ( `${ CollectionConstants . INFO }/${ CollectionConstants . CONNECTED }` ) . valueChanges ( ) . subscribe ( connected => { this . isUserOnline = connected ? true : false ; if ( ! this . isUserOnline ) { this . updateTokenStatus ( user . userId , UserStatusConstants . OFFLINE ) ; return ; } this . updateOnDisconnect ( user ) ; } ) ; } private updateOnDisconnect ( user ) { this . db . database . ref ( `${ CollectionConstants . USERS }/${ user . userId }` ) . onDisconnect ( ) . update ( { status : UserStatusConstants . OFFLINE } ) . then ( ( ) => { this . updateTokenStatus ( user . userId , UserStatusConstants . ONLINE ) ; } ) ; } public updateTokenStatus ( userId , status ) { if ( this . isUserOnline ) { this . db . object ( `/${ CollectionConstants . USERS }/${ userId }` ) . set ( { status , userId , device : TriggerConstants . WEB , lastUpdated : new Date ( ) . getTime ( ) } ) ; } } }