import { Inject , Injectable , PLATFORM_ID } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { AngularFireLiteApp } from '../core.service' ; import { isPlatformBrowser , isPlatformServer } from '@angular/common' ; import { from , Observable , Subject } from 'rxjs' ; import { FirebaseAuth } from '@firebase/auth-types' ; import 'firebase/auth' ; @ Injectable ( ) export class AngularFireLiteAuth { private readonly auth : FirebaseAuth ; private readonly config ; private readonly browser = isPlatformBrowser ( this . platformId ) ; private readonly server = isPlatformServer ( this . platformId ) ; constructor ( private app , private http , @ Inject ( PLATFORM_ID ) private platformId ) { this . auth = app . instance ( ) . auth ( ) ; this . config = app . config ( ) ; } uid ( ) { const UID = new Subject ( ) ; this . isAuthenticated ( ) . subscribe ( ( isAuth ) => { if ( isAuth ) { this . auth . onAuthStateChanged ( ( user ) => { UID . next ( user . uid ) ; } ) ; } else { UID . next ( null ) ; } } ) ; return UID ; } isAuthenticated ( ) { const IS_AUTHENTICATED = new Subject ( ) ; this . auth . onAuthStateChanged ( ( user ) => { IS_AUTHENTICATED . next ( ! ! user ) ; } ) ; return IS_AUTHENTICATED ; } isAnonymous ( ) { const IS_ANONYMOUS = new Subject ( ) ; this . auth . onAuthStateChanged ( ( user ) => { IS_ANONYMOUS . next ( user . isAnonymous ) ; } ) ; return IS_ANONYMOUS ; } currentUser ( ) { const CURRENT_USER = new Subject ( ) ; CURRENT_USER . next ( this . auth . currentUser ) ; return CURRENT_USER ; } userData ( ) { if ( this . server ) { return from ( this . auth . currentUser . getIdToken ( true ) . then ( ( idToken ) => { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=${ this . config . apiKey }` , { 'idToken' : idToken } ) ; } ) ) ; } if ( this . browser ) { const USER_DATA = new Subject ( ) ; this . auth . onAuthStateChanged ( ( user ) => { if ( user ) { USER_DATA . next ( { displayName : user . displayName , email : user . email , emailVerified : user . emailVerified , photoURL : user . photoURL , isAnonymous : user . isAnonymous , uid : user . uid , providerData : user . providerData , phoneNumber : user . phoneNumber } ) ; } } ) ; return USER_DATA ; } } providers ( email ) { if ( this . server ) { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/createAuthUri?key=${ this . config . apiKey }` , { 'identifier' : email , } ) ; } if ( this . browser ) { const PROVIDERS = new Subject ( ) ; this . auth . fetchSignInMethodsForEmail ( email ) . then ( ( ( providers ) => { PROVIDERS . next ( providers ) ; } ) ) ; return PROVIDERS ; } } signin ( email , password ) { if ( this . server ) { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=${ this . config . apiKey }` , { 'email' : email , 'password' : password , 'returnSecureToken' : true } ) ; } if ( this . browser ) { return from ( this . auth . signInWithEmailAndPassword ( email , password ) ) ; } } signinAnonymously ( ) { if ( this . server ) { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=${ this . config . apiKey }` , { 'returnSecureToken' : true } ) ; } if ( this . browser ) { return from ( this . auth . signInAnonymously ( ) ) ; } } signup ( email , password ) { if ( this . server ) { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=${ this . config . apiKey }` , { 'email' : email , 'password' : password , 'returnSecureToken' : true } ) ; } if ( this . browser ) { return from ( this . auth . createUserWithEmailAndPassword ( email , password ) ) ; } } signout ( ) { return from ( this . auth . signOut ( ) ) ; } updateProfile ( data ) { if ( this . server ) { let deleteAttribute ; if ( data . displayName === null && data . photoURL === null ) { deleteAttribute = '\'PHOTO_URL\' , \'DISPLAY_NAME\'' ; } else if ( data . displayName === null ) { deleteAttribute = 'DISPLAY_NAME' ; } else if ( data . photoURL === null ) { deleteAttribute = 'PHOTO_URL' ; } return this . auth . currentUser . getIdToken ( true ) . then ( ( idToken ) => { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=${ this . config . apiKey }` , { 'idToken' : idToken , 'displayName' : data . displayName , 'photoUrl' : data . photoURL , 'deleteAttribute' : deleteAttribute , 'returnSecureToken' : true } ) ; } ) ; } if ( this . browser ) { return from ( this . auth . currentUser . updateProfile ( data ) ) ; } } updateEmail ( newEmail ) { if ( this . server ) { return this . auth . currentUser . getIdToken ( true ) . then ( ( idToken ) => { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=${ this . config . apiKey }` , { 'idToken' : idToken , 'email' : newEmail , 'returnSecureToken' : true } ) ; } ) ; } if ( this . browser ) { return from ( this . auth . currentUser . updateEmail ( newEmail ) ) ; } } updatePassword ( newPassword ) { if ( this . server ) { return this . auth . currentUser . getIdToken ( true ) . then ( ( idToken ) => { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=${ this . config . apiKey }` , { 'idToken' : idToken , 'password' : newPassword , 'returnSecureToken' : true } ) ; } ) ; } if ( this . browser ) { return from ( this . auth . currentUser . updatePassword ( newPassword ) ) ; } } verifyPasswordResetCode ( code ) { if ( this . server ) { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/resetPassword?key=${ this . config . apiKey }` , { 'oobCode' : code } ) ; } if ( this . browser ) { return from ( this . auth . verifyPasswordResetCode ( code ) ) ; } } confirmPasswordReset ( code , newPassword ) { if ( this . server ) { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/resetPassword?key=${ this . config . apiKey }` , { 'oobCode' : code , 'newPassword' : newPassword } ) ; } if ( this . browser ) { return from ( this . auth . confirmPasswordReset ( code , newPassword ) ) ; } } relogin ( credentials ) { return from ( this . auth . currentUser . reauthenticateWithCredential ( credentials ) ) ; } deletePermanently ( ) { if ( this . server ) { return from ( this . auth . currentUser . getIdToken ( true ) . then ( ( idToken ) => { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=${ this . config . apiKey }` , { 'idToken' : idToken } ) ; } ) ) ; } if ( this . browser ) { return from ( this . auth . currentUser . delete ( ) ) ; } } sendEmailVerification ( ) { if ( this . server ) { return from ( this . auth . currentUser . getIdToken ( true ) . then ( ( idToken ) => { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=${ this . config . apiKey }` , { 'requestType' : 'VERIFY_EMAIL' , 'idToken' : idToken } ) ; } ) ) ; } if ( this . browser ) { return from ( this . auth . currentUser . sendEmailVerification ( ) ) ; } } sendPasswordResetEmail ( email ) { if ( this . server ) { return this . http . post ( `https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=${ this . config . apiKey }` , { 'requestType' : 'PASSWORD_RESET' , 'email' : email } ) ; } if ( this . browser ) { return from ( this . auth . sendPasswordResetEmail ( email ) ) ; } } }