Last active
March 17, 2021 20:53
-
-
Save evlymn/b5d776068791ccfa30dad8b1901480e2 to your computer and use it in GitHub Desktop.
Service angular com métodos usados no Firebase Authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NgZone } from '@angular/core'; | |
import { Router } from '@angular/router'; | |
import { AngularFireAuth } from '@angular/fire/auth'; | |
import * as firebase from 'firebase/app'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class AuthenticationService { | |
private auth: firebase.auth.Auth; | |
// tslint:disable-next-line: variable-name | |
private _userClaims: any; | |
constructor( | |
private angularFireAuth: AngularFireAuth, | |
private router: Router, | |
private zone: NgZone | |
) { | |
this.auth = this.angularFireAuth.auth; | |
this.onAuthStateChanged(); | |
this.onIdTokenChanged(); | |
} | |
public get providerId() { | |
return { | |
Email: firebase.auth.EmailAuthProvider.PROVIDER_ID, | |
Google: firebase.auth.GoogleAuthProvider.PROVIDER_ID, | |
GitHub: firebase.auth.GithubAuthProvider.PROVIDER_ID, | |
Twitter: firebase.auth.TwitterAuthProvider.PROVIDER_ID, | |
Facebook: firebase.auth.FacebookAuthProvider.PROVIDER_ID | |
}; | |
} | |
public get persistence() { | |
return { | |
LOCAL: firebase.auth.Auth.Persistence.LOCAL, | |
NONE: firebase.auth.Auth.Persistence.NONE, | |
SESSION: firebase.auth.Auth.Persistence.SESSION | |
}; | |
} | |
public get currenUser(): firebase.User { | |
return this.auth.currentUser; | |
} | |
public get userClaims(): any { | |
return this._userClaims; | |
} | |
private onIdTokenChanged() { | |
this.auth.onIdTokenChanged(user => { | |
this.setUserClaims(user); | |
}); | |
} | |
private onAuthStateChanged() { | |
this.auth.onAuthStateChanged(user => { | |
if (user) { | |
this.setUserClaims(user); | |
} else { | |
console.log('logout'); | |
} | |
// Custom function call | |
// this.setCustomAppState(user); | |
}); | |
} | |
// Custom function definition example | |
private setCustomAppState(user: firebase.User) { | |
const loggedOutRoute = '/login'; | |
if (user) { | |
if (user.emailVerified) { | |
this.setCustomAppRoute('/'); | |
} else { | |
this.setCustomAppRoute(loggedOutRoute); | |
} | |
} else { | |
this.setCustomAppRoute(loggedOutRoute); | |
} | |
} | |
// Custom function definition example | |
private setCustomAppRoute(route: string) { | |
this.zone.run(() => { | |
this.router.navigate([route]); | |
}); | |
} | |
private setUserClaims(user: firebase.User) { | |
user.getIdTokenResult().then(idTokenResult => { | |
this._userClaims = idTokenResult.claims; | |
}); | |
} | |
getUserStatus() { | |
return new Promise<firebase.User>((resolve, reject) => { | |
if (this.currenUser) { | |
resolve(this.currenUser); | |
} | |
this.auth.onAuthStateChanged(user => { | |
if (user) { | |
resolve(user as firebase.User); | |
} else { | |
reject(Error('user null')); | |
} | |
}); | |
}); | |
} | |
setPersistence(persistence: firebase.auth.Auth.Persistence) { | |
this.auth.setPersistence(persistence); | |
} | |
signInWithGithubAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.auth.signInWithPopup(new firebase.auth.GithubAuthProvider()); | |
} | |
signInWithGoogleAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()); | |
} | |
signInWithTwitterAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.auth.signInWithPopup(new firebase.auth.TwitterAuthProvider()); | |
} | |
signInWithFacebookAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider()); | |
} | |
signInWithEmailAndPassword( | |
email: string, | |
password: string | |
): Promise<firebase.auth.UserCredential> { | |
return this.auth.signInWithEmailAndPassword(email, password); | |
} | |
signInAnonymously(): Promise<firebase.auth.UserCredential> { | |
return this.auth.signInAnonymously(); | |
} | |
linkWithGithubAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.currenUser.linkWithPopup(new firebase.auth.GithubAuthProvider()); | |
} | |
unLinkGithubAuthProvider(): Promise<firebase.User> { | |
return this.currenUser.unlink(new firebase.auth.GithubAuthProvider().providerId); | |
} | |
linkWithGoogleAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.currenUser.linkWithPopup(new firebase.auth.GoogleAuthProvider()); | |
} | |
unLinkGoogleAuthProvider(): Promise<firebase.User> { | |
return this.currenUser.unlink(new firebase.auth.GoogleAuthProvider().providerId); | |
} | |
linkWithTwitterAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.currenUser.linkWithPopup(new firebase.auth.TwitterAuthProvider()); | |
} | |
unLinkTwitterAuthProvider(): Promise<firebase.User> { | |
return this.currenUser.unlink(new firebase.auth.TwitterAuthProvider().providerId); | |
} | |
linkWithFacebookAuthProvider(): Promise<firebase.auth.UserCredential> { | |
return this.currenUser.linkWithPopup(new firebase.auth.FacebookAuthProvider()); | |
} | |
unLinkFacebookAuthProvider(): Promise<firebase.User> { | |
return this.currenUser.unlink(new firebase.auth.FacebookAuthProvider().providerId); | |
} | |
verifySignInMethodForEmail(email: string, providerId: string) { | |
return new Promise<{ signin: boolean; methods: string[] }>((resolve, reject) => { | |
this.fetchSignInMethodsForEmail(email) | |
.then(list => { | |
if (list.includes(providerId)) { | |
resolve({ signin: true, methods: list }); | |
} else { | |
resolve({ signin: false, methods: list }); | |
} | |
}) | |
.catch(reason => reject(reason)); | |
}); | |
} | |
fetchSignInMethodsForEmail(email: string): Promise<string[]> { | |
return this.auth.fetchSignInMethodsForEmail(email); | |
} | |
createUserWithEmailAndPassword( | |
email: string, | |
password: string | |
): Promise<firebase.auth.UserCredential> { | |
return this.auth.createUserWithEmailAndPassword(email, password); | |
} | |
sendPasswordResetEmail( | |
email: string, | |
actionCodeSettings?: firebase.auth.ActionCodeSettings | |
): Promise<void> { | |
return this.auth.sendPasswordResetEmail(email, actionCodeSettings); | |
} | |
signOut() { | |
return this.auth.signOut(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependências