src/auth/module/services/auth-process.service.ts
Properties |
Methods |
|
constructor(auth: AngularFireAuth, alertService: AlertService, _fireStoreService: FirestoreSyncService)
|
||||||||||||
Parameters :
|
Public Async deleteAccount |
deleteAccount()
|
Returns :
Promise<any>
|
Public getPhotoPath | ||||||
getPhotoPath(image: string)
|
||||||
Parameters :
Returns :
string
|
Public getUserPhotoUrl |
getUserPhotoUrl()
|
Returns :
string
|
handleError | ||||||
handleError(error: any)
|
||||||
Parameters :
Returns :
void
|
Async handleSuccess | ||||||
handleSuccess(userCredential: UserCredential)
|
||||||
Parameters :
Returns :
any
|
Public parseUserInfo | ||||||
parseUserInfo(user: User)
|
||||||
Parameters :
Returns :
UserInfo
|
Public resetPassword | ||||||||
resetPassword(email: string)
|
||||||||
Reset the password of the user via email
Parameters :
Returns :
any
|
Public Async signInWith | ||||||||||||||||
signInWith(provider: AuthProvider, email?: string, password?: string)
|
||||||||||||||||
General sign in mechanism to authenticate the users with a firebase project using a traditional way, via username and password or by using an authentication provider like google, facebook, twitter and github
Parameters :
Returns :
any
|
Public signInWithPhoneNumber |
signInWithPhoneNumber()
|
Returns :
void
|
Public Async signUp | ||||||||||||||||
signUp(name: string, email: string, password: string)
|
||||||||||||||||
Sign up new users via email and password. After that the user should verify and confirm an email sent via the firebase
Parameters :
Returns :
any
|
Public Async updateProfile | ||||||||||||
updateProfile(name: string, photoURL: string)
|
||||||||||||
Update the profile (name + photo url) of the authenticated user in the firebase authentication feature (not in firestore)
Parameters :
Returns :
Promise<any>
|
Public alertService |
alertService:
|
Type : AlertService
|
Public auth |
auth:
|
Type : AngularFireAuth
|
emailConfirmationSent |
emailConfirmationSent:
|
Type : boolean
|
emailToConfirm |
emailToConfirm:
|
Type : string
|
isLoading |
isLoading:
|
Type : boolean
|
onErrorEmitter |
onErrorEmitter:
|
Type : EventEmitter<any>
|
Default value : new EventEmitter<any>()
|
onSuccessEmitter |
onSuccessEmitter:
|
Type : EventEmitter<any>
|
Default value : new EventEmitter<any>()
|
import {EventEmitter, Injectable} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {ISignInProcess, ISignUpProcess} from '../interfaces/main.interface';
import {FirestoreSyncService} from './firestore-sync.service';
import * as firebase from 'firebase';
import {User, UserInfo} from 'firebase';
import {Accounts} from '../components/enums';
// import User = firebase.User;
import GoogleAuthProvider = firebase.auth.GoogleAuthProvider;
import FacebookAuthProvider = firebase.auth.FacebookAuthProvider;
import TwitterAuthProvider = firebase.auth.TwitterAuthProvider;
import UserCredential = firebase.auth.UserCredential;
import GithubAuthProvider = firebase.auth.GithubAuthProvider;
import {AlertService} from './alert.service';
export enum AuthProvider {
ALL = 'all',
ANONYMOUS = 'anonymous',
EmailAndPassword = 'firebase',
Google = 'google',
Facebook = 'facebook',
Twitter = 'twitter',
Github = 'github',
PhoneNumber = 'phoneNumber'
}
@Injectable()
export class AuthProcessService implements ISignInProcess, ISignUpProcess {
onSuccessEmitter: EventEmitter<any> = new EventEmitter<any>();
onErrorEmitter: EventEmitter<any> = new EventEmitter<any>();
isLoading: boolean;
emailConfirmationSent: boolean;
emailToConfirm: string;
constructor(public auth: AngularFireAuth,
public alertService: AlertService,
private _fireStoreService: FirestoreSyncService) {
}
/**
* Reset the password of the user via email
*
* @param email - the email to reset
* @returns
*/
public resetPassword(email: string) {
this.isLoading = true;
return this.auth.auth.sendPasswordResetEmail(email)
.then(() => {
this.isLoading = false;
return;
})
.catch((error) => {
this.isLoading = false;
return this.onErrorEmitter.next(error);
});
}
/**
* General sign in mechanism to authenticate the users with a firebase project
* using a traditional way, via username and password or by using an authentication provider
* like google, facebook, twitter and github
*
* @param provider - the provider to authenticate with (google, facebook, twitter, github)
* @param email - (optional) the email of user - used only for a traditional sign in
* @param password - (optional) the password of user - used only for a traditional sign in
* @returns
*/
public async signInWith(provider: AuthProvider, email?: string, password?: string) {
try {
this.isLoading = true;
let signInResult: UserCredential;
switch (provider) {
case AuthProvider.ANONYMOUS:
signInResult = await this.auth.auth.signInAnonymously() as UserCredential;
break;
case AuthProvider.EmailAndPassword:
signInResult = await this.auth.auth.signInWithEmailAndPassword(email, password) as UserCredential;
break;
case AuthProvider.Google:
signInResult = await this.auth.auth.signInWithPopup(new GoogleAuthProvider()) as UserCredential;
break;
case AuthProvider.Facebook:
signInResult = await this.auth.auth.signInWithPopup(new FacebookAuthProvider()) as UserCredential;
break;
case AuthProvider.Twitter:
signInResult = await this.auth.auth.signInWithPopup(new TwitterAuthProvider()) as UserCredential;
break;
case AuthProvider.Github:
signInResult = await this.auth.auth.signInWithPopup(new GithubAuthProvider()) as UserCredential;
break;
default:
throw new Error(`${AuthProvider[provider]} is not available as auth provider`);
}
await this.handleSuccess(signInResult);
} catch (err) {
this.handleError(err);
console.error(err);
this.alertService.onNewAlert.emit(
{
id: new Date().getTime(),
message: err.message,
type: 'danger'
});
this.onErrorEmitter.next(err);
} finally {
this.isLoading = false;
}
}
/**
* Sign up new users via email and password.
* After that the user should verify and confirm an email sent via the firebase
*
* @param name - the name if the new user
* @param email - the email if the new user
* @param password - the password if the new user
* @returns
*/
public async signUp(name: string, email: string, password: string) {
try {
this.isLoading = true;
const userCredential: UserCredential = await this.auth.auth.createUserWithEmailAndPassword(email, password);
const user = userCredential.user;
await this._fireStoreService
.getUserDocRefByUID(user.uid)
.set({
uid: user.uid,
displayName: name,
email: user.email,
photoURL: user.photoURL
} as firebase.User);
await user.sendEmailVerification();
await this.updateProfile(name, user.photoURL);
this.emailConfirmationSent = true;
this.emailToConfirm = email;
await this.handleSuccess(userCredential);
} catch (err) {
console.error(err);
this.handleError(err);
} finally {
this.isLoading = false;
}
}
/**
* Update the profile (name + photo url) of the authenticated user in the
* firebase authentication feature (not in firestore)
*
* @param name - the new name of the authenticated user
* @param photoURL - the new photo url of the authenticated user
* @returns
*/
public async updateProfile(name: string, photoURL: string): Promise<any> {
return await this.auth.auth.currentUser.updateProfile({displayName: name, photoURL: photoURL});
}
public async deleteAccount(): Promise<any> {
return await this.auth.auth.currentUser.delete();
}
public parseUserInfo(user: User): UserInfo {
return {
uid: user.uid,
displayName: user.displayName,
email: user.email,
phoneNumber: user.phoneNumber,
photoURL: user.photoURL,
providerId: user.providerData.length > 0 ? user.providerData[0].providerId : null
};
}
public getUserPhotoUrl(): string {
const user: firebase.User | null = this.auth.auth.currentUser;
if (user.photoURL) {
return user.photoURL;
} else if (user.emailVerified) {
return this.getPhotoPath(Accounts.CHECK);
} else if (user.isAnonymous) {
return this.getPhotoPath(Accounts.OFF);
} else {
return this.getPhotoPath(Accounts.NONE);
}
}
public getPhotoPath(image: string) {
return `assets/user/${image}.svg`;
}
public signInWithPhoneNumber() {
// todo: 3.1.18
}
async handleSuccess(userCredential: UserCredential) {
await this._fireStoreService.updateUserData(this.parseUserInfo(userCredential.user));
this.alertService.onNewAlert.emit(
{
id: new Date().getTime(),
message: `Hallo ${userCredential.user.displayName ? userCredential.user.displayName : ''}!`,
type: 'success'
});
this.onSuccessEmitter.next(userCredential.user);
}
handleError(error: any) {
this.alertService.onNewAlert.emit(
{
id: new Date().getTime(),
message: error.message,
type: 'danger'
});
console.error(error);
this.onErrorEmitter.next(error);
}
}