Simple lightweight Firebase Authentication Service for Angular 12+
npm install ngx-firebase-auth@angular v13.2.1npm install ngx-firebase-auth@angular/core | >=11.0.x <=13.2.x |@angular/fire | 7.2.0 |firebase| 9.6.6 |rxjs| >=6.5.x <=7.4.x |
constructor(private authService: NgxFirebaseAuthService) {
// some code
}
`To register
`
/*
Assume that there are two inputs in HTML (email and password) and
we call the function on button press with the input values as parameters
*/private register(emailInput: string, passwordInput: string): void {
const context: AuthContext = {
email: emailInput,
password: passwordInput,
};
this.authService.register(context).then((user: UserCredential) => {
console.log(user);
}).catch((e) => {
console.error(e);
});
}
`
To log in
`
/*
Assume that there are two inputs in HTML (email and password) and
we call the function on button press with the input values as parameters
*/private (emailInput: string, passwordInput: string): void {
const context: AuthContext = {
email: emailInput,
password: passwordInput,
};
this.authService.login(context).then((user: UserCredential) => {
console.log(user);
}).catch((e) => {
console.error(e);
});
}
`Functions
Note:
UserCredential = firebase.auth.UserCredential
FirebaseUser = firebase.User|Type | Name | Description | Return Value |
|---|---|---|---|
|
getter | currentUser$ | Get the current User Observable from AngularFireAuth | Observable |
| getter |currentUser | Gets the current user if authenticated | FirebaseUser or null |
| getter |currentUserId | Gets the current user id if authenticated | string or null |
| getter |authenticated | Checks if user is authenticated | boolean |
| getter |isVerified | Checks if user email is verified | boolean |
| function|register(context: AuthContext) | Register the user | Promise |
| function|login(context: AuthContext) | Login the user | Promise |
| function|logout() | Logs out the user and clear credentials. | Promise |
| function|sendEmailVerification() | Sends Email Verification e.g. after registration. | Promise |
| function|sendPasswordResetEmail(email: string) | Sends reset password mail | Promise |
| function|reauthenticateUser(password: string) | Reauthenticate an user, e.g. when updating user email | Promise |Interfaces
`
interface AuthContext {
email: string;
password: string;
}
`
FAQ
$3
Add the following line in your main tsconfig.json inside compilerOptions:"skipLibcheck": true`Feel free to provide a PR | open an appropriate issue here
If you like this project, support ngx-firebase-auth by starring :star: and sharing it :loudspeaker: