ngx-firebase-auth
Version:
Simple lightweight Firebase Authentication Service for Angular 12+
147 lines (141 loc) • 5.34 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, NgModule } from '@angular/core';
import firebase from 'firebase/compat/app';
import { shareReplay } from 'rxjs/operators';
import * as i1 from '@angular/fire/compat/auth';
class NgxFirebaseAuthService {
constructor(afAuth) {
this.afAuth = afAuth;
this.authState = null;
this.afAuth.authState.subscribe((authState) => {
this.authState = authState;
});
}
/**
* Get the current User Observable from AngularFireAuth
* @return Observable<FirebaseUser> if the user is authenticated.
*/
get currentUser$() {
return this.afAuth.authState.pipe(shareReplay());
}
/**
* Gets the current user.
* @return The user or null if the user is not authenticated.
*/
get currentUser() {
return this.authState;
}
/**
* Gets the current user id.
* @return The user id or null if the user is not authenticated.
*/
get currentUserId() {
return this.currentUser ? this.currentUser.uid : '';
}
/**
* Checks is the user is authenticated.
* @return True if the user is authenticated.
*/
get authenticated() {
return this.authState != null;
}
/**
* Checks if the user email is verified.
* @return True if the user is authorized.
*/
get isVerified() {
return this.currentUser && this.currentUser.emailVerified;
}
/**
* Register the user.
* @param context The register parameters.
* @return The user credentials.
*/
register(context) {
return this.afAuth.createUserWithEmailAndPassword(context.email, context.password);
}
/**
* Login the user.
* @param context The login parameters.
* @return The user credentials.
*/
login(context) {
return this.afAuth.signInWithEmailAndPassword(context.email, context.password);
}
/**
* Logs out the user and clear credentials.
* @return void
*/
logout() {
// Customize credentials invalidation here
return this.afAuth.signOut();
}
/**
* Sends Email Verification e.g. after registration.
* @return void
*/
sendEmailVerification() {
const currentUser = firebase.auth().currentUser;
if (currentUser) {
return currentUser.sendEmailVerification();
}
return new Promise(async (_, reject) => reject('Could not call sendEmailVerification - No User available!'));
}
/**
* Sends reset password mail
* @return void
*/
sendPasswordResetEmail(email) {
return this.afAuth.sendPasswordResetEmail(email);
}
/**
* Reauthenticate an user, e.g. when updating user email
* @return return new firebase user
*/
reauthenticateUser(password) {
const firebaseUser = this.currentUser;
if (firebaseUser?.email != null) {
const credentials = firebase.auth.EmailAuthProvider.credential(firebaseUser.email, password);
return new Promise((resolve, reject) => {
firebaseUser.reauthenticateWithCredential(credentials).then(() => {
resolve(firebaseUser);
}).catch((err) => {
console.log(err);
reject('Verification failed');
});
});
}
return new Promise((_, reject) => reject('Could not call reauthenticateUser - No User available!'));
}
}
NgxFirebaseAuthService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.1", ngImport: i0, type: NgxFirebaseAuthService, deps: [{ token: i1.AngularFireAuth }], target: i0.ɵɵFactoryTarget.Injectable });
NgxFirebaseAuthService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.2.1", ngImport: i0, type: NgxFirebaseAuthService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.1", ngImport: i0, type: NgxFirebaseAuthService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}], ctorParameters: function () { return [{ type: i1.AngularFireAuth }]; } });
class NgxFirebaseAuthModule {
}
NgxFirebaseAuthModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.1", ngImport: i0, type: NgxFirebaseAuthModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NgxFirebaseAuthModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.1", ngImport: i0, type: NgxFirebaseAuthModule });
NgxFirebaseAuthModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.1", ngImport: i0, type: NgxFirebaseAuthModule, providers: [
NgxFirebaseAuthService,
] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.1", ngImport: i0, type: NgxFirebaseAuthModule, decorators: [{
type: NgModule,
args: [{
providers: [
NgxFirebaseAuthService,
],
}]
}] });
/*
* Public API Surface of ngx-firebase-auth
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxFirebaseAuthModule, NgxFirebaseAuthService };
//# sourceMappingURL=ngx-firebase-auth.mjs.map