import { Injectable } from '@angular/core' ; import { AngularFireAuth } from '@angular/fire/auth' ; import { Observable , Subject , from } from 'rxjs' ; import { Platform } from '@ionic/angular' ; import { User , auth } from 'firebase/app' ; import { ProfileModel } from './profile/profile.model' ; import { filter , map , take } from 'rxjs/operators' ; @ Injectable ( ) export class FirebaseAuthService { currentUser : User ; userProviderAdditionalInfo : any ; redirectResult : Subject < any > = new Subject < any > ( ) ; constructor ( public angularFireAuth , public platform ) { this . angularFireAuth . onAuthStateChanged ( ( user ) => { if ( user ) { this . currentUser = user ; } else { this . currentUser = null ; } } ) ; this . angularFireAuth . getRedirectResult ( ) . then ( ( result ) => { if ( result . user ) { this . setProviderAdditionalInfo ( result . additionalUserInfo . profile ) ; this . currentUser = result . user ; this . redirectResult . next ( result ) ; } } , ( error ) => { this . redirectResult . next ( { error : error . code } ) ; } ) ; } getRedirectResult ( ) { return this . redirectResult . asObservable ( ) ; } setProviderAdditionalInfo ( additionalInfo ) { this . userProviderAdditionalInfo = { ... additionalInfo } ; } public getProfileDataSource ( ) { return this . angularFireAuth . user . pipe ( filter ( ( user ) => user != null ) , map ( ( user ) => { return this . getProfileData ( ) ; } ) , take ( 1 ) ) ; } public getProfileData ( ) { const userModel = new ProfileModel ( ) ; let providerData = this . currentUser . providerData [ 0 ] ; if ( this . userProviderAdditionalInfo ) { providerData = { ... providerData , ... this . userProviderAdditionalInfo } ; } switch ( providerData . providerId ) { case 'facebook.com' : userModel . image = providerData . photoURL + '?height=400' ; break ; case 'password' : userModel . image = 'https://s3-us-west-2.amazonaws.com/ionicthemes/otros/avatar-placeholder.png' ; break ; case 'twitter.com' : userModel . image = providerData . photoURL . replace ( '_normal' , '_400x400' ) ; break ; case 'google.com' : userModel . image = providerData . photoURL . split ( '=' ) [ 0 ] ; break ; default : userModel . image = providerData . photoURL ; } userModel . name = providerData . name || providerData . displayName || 'What\'s your name?' ; userModel . role = 'How would you describe yourself?' ; userModel . description = providerData . description || 'Anything else you would like to share with the world?' ; userModel . phoneNumber = providerData . phoneNumber || 'Is there a number where I can reach you?' ; userModel . email = providerData . email || 'Where can I send you emails?' ; userModel . provider = ( providerData . providerId !== 'password' ) ? providerData . providerId : 'Credentials' ; return userModel ; } signOut ( ) { return from ( this . angularFireAuth . signOut ( ) ) ; } signInWithEmail ( email , password ) { return this . angularFireAuth . signInWithEmailAndPassword ( email , password ) ; } signUpWithEmail ( email , password ) { return this . angularFireAuth . createUserWithEmailAndPassword ( email , password ) ; } socialSignIn ( providerName , scopes ? ) { const provider = new auth . OAuthProvider ( providerName ) ; if ( scopes ) { scopes . forEach ( scope => { provider . addScope ( scope ) ; } ) ; } if ( this . platform . is ( 'desktop' ) ) { return this . angularFireAuth . signInWithPopup ( provider ) ; } else { return this . angularFireAuth . signInWithRedirect ( provider ) ; } } signInWithFacebook ( ) { const provider = new auth . FacebookAuthProvider ( ) ; return this . socialSignIn ( provider . providerId ) ; } signInWithGoogle ( ) { const provider = new auth . GoogleAuthProvider ( ) ; const scopes = [ 'profile' , 'email' ] ; return this . socialSignIn ( provider . providerId , scopes ) ; } signInWithTwitter ( ) { const provider = new auth . TwitterAuthProvider ( ) ; return this . socialSignIn ( provider . providerId ) ; } }