import { Injectable } from '@angular/core' ; import { FirebaseAuthService } from './../firebase-auth.service' ; import * as firebaseApp from 'nativescript-plugin-firebase/app' ; import * as firebase from 'nativescript-plugin-firebase' ; import { Subject , Observable } from 'rxjs' ; import { RouterExtensions } from 'nativescript-angular/router' ; import { User , CollectionConstants , UserStatusConstants , TriggerConstants } from 'shared-library/shared/model' ; import { isAndroid } from 'tns-core-modules/ui/page/page' ; @ Injectable ( ) export class TNSFirebaseAuthService implements FirebaseAuthService { private userSubject : Subject < firebase . User > ; private pushToken : string ; private user : any ; constructor ( private routerExtension ) { this . userSubject = new Subject < firebase . User > ( ) ; firebase . addAuthStateListener ( { onAuthStateChanged : ( data ) => { this . user = data . user ; return this . userSubject . next ( data . user ) ; } } ) ; } public createUserWithEmailAndPassword ( email , password ) { return this . firebaseAuth ( ) . createUserWithEmailAndPassword ( email , password ) ; } public showLogin ( ) { this . routerExtension . navigate ( [ '/login' ] , { clearHistory : true } ) ; } public authState ( ) { return this . userSubject . asObservable ( ) ; } public getIdToken ( user , forceRefresh ) { return firebase . getAuthToken ( { forceRefresh : forceRefresh } ) ; } public signOut ( ) { this . updateTokenStatus ( this . user . uid , UserStatusConstants . OFFLINE ) ; this . updatePushToken ( undefined ) ; this . firebaseAuth ( ) . signOut ( ) ; } public firebaseAuth ( ) { return firebaseApp . auth ( ) ; } public async refreshToken ( forceRefresh ) { const token = await firebase . getAuthToken ( { forceRefresh : forceRefresh } ) ; return token . token ; } public async updatePassword ( email , currentPassword , newPassword ) { try { await this . firebaseAuth ( ) . updatePassword ( newPassword ) ; await firebase . reauthenticate ( { type : firebase . LoginType . PASSWORD , passwordOptions : { email : email , password : newPassword } } ) ; return 'success' ; } catch ( error ) { console . log ( 'error---->' , error ) ; throw error ; } } public signInWithEmailAndPassword ( email , password ) { return this . firebaseAuth ( ) . signInWithEmailAndPassword ( email , password ) ; } public sendEmailVerification ( user ) { return firebase . sendEmailVerification ( ) ; } public sendPasswordResetEmail ( email ) { return firebase . sendPasswordResetEmail ( email ) ; } public googleLogin ( ) { return firebase . login ( { type : firebase . LoginType . GOOGLE } ) ; } public facebookLogin ( ) { return firebase . login ( { type : firebase . LoginType . FACEBOOK , facebookOptions : { scopes : [ 'public_profile' , 'email' ] } } ) ; } public appleLogin ( ) { return firebase . login ( { type : firebase . LoginType . APPLE , appleOptions : { locale : "en" , scopes : [ "email" ] } } ) ; } public phoneLogin ( phoneNumber ) { return firebase . login ( { type : firebase . LoginType . PHONE , phoneOptions : { phoneNumber : phoneNumber , verificationPrompt : 'Enter received verification code' } } ) ; } public twitterLogin ( ) { return new Promise ( resolve => resolve ( ) ) ; } public githubLogin ( ) { return new Promise ( resolve => resolve ( ) ) ; } public resumeState ( user ) { this . userSubject . next ( user ) ; } public updatePushToken ( token ) { this . pushToken = token ; } public updateOnConnect ( user ) { firebaseApp . database ( ) . ref ( `${ CollectionConstants . INFO }/${ CollectionConstants . CONNECTED }` ) . on ( 'value' , ( snapshot ) => { if ( ! snapshot . val ( ) ) { this . updateTokenStatus ( user . userId , UserStatusConstants . OFFLINE ) ; return ; } this . updateOnDisconnect ( user ) ; } ) ; } private updateOnDisconnect ( user ) { firebaseApp . database ( ) . ref ( `${ CollectionConstants . USERS }/${ this . pushToken }` ) . onDisconnect ( ) . update ( { status : UserStatusConstants . OFFLINE } ) . then ( ( ) => { this . updateTokenStatus ( user . userId , UserStatusConstants . ONLINE ) ; } ) ; } public updateTokenStatus ( userId , status ) { firebaseApp . database ( ) . ref ( `/${ CollectionConstants . USERS }/${ this . pushToken }` ) . set ( { status , userId , device : ( isAndroid ) ? TriggerConstants . ANDROID : TriggerConstants . IOS } ) ; } }