Created
February 25, 2019 19:01
-
-
Save Sampath-Lokuge/6d1adc2dafeee83ffb839c96fb08bb13 to your computer and use it in GitHub Desktop.
Authentication
This file contains hidden or 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 } from '@angular/core'; | |
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore'; | |
import { AngularFireAuth } from '@angular/fire/auth'; | |
import { ShowToastService } from './show-toast.service'; | |
import * as firebase from 'firebase/app'; | |
import { UserProfile } from '../models/user-profile'; | |
import { Facebook } from '@ionic-native/facebook/ngx'; | |
import { Observable } from 'rxjs'; | |
import { first } from 'rxjs/operators'; | |
import { GooglePlus } from '@ionic-native/google-plus/ngx'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class AuthenticationService { | |
constructor(private afAuth: AngularFireAuth, | |
private fireStore: AngularFirestore, | |
private showToastService: ShowToastService, | |
private facebook: Facebook, | |
private googlePlus: GooglePlus) { } | |
getUser(): firebase.User { | |
return this.afAuth.auth.currentUser; | |
} | |
async signUp(name: string, email: string, password: string): Promise<void> { | |
try { | |
const user: firebase.auth.UserCredential = await this.afAuth.auth.createUserWithEmailAndPassword(email, password); | |
const userProfileDocument: AngularFirestoreDocument<UserProfile> = this.fireStore.doc(`userProfiles/${user.user.uid}`); | |
const userProfile: UserProfile = { | |
id: user.user.uid, | |
email: email, | |
creationTime: firebase.firestore.Timestamp, | |
name: name | |
}; | |
await userProfileDocument.set(userProfile); | |
} catch (error) { | |
console.error(error); | |
this.showToastService.showErrorToast(error.message); | |
} | |
} | |
async signIn(email: string, password: string): Promise<firebase.auth.UserCredential> { | |
try { | |
return await this.afAuth.auth.signInWithEmailAndPassword(email, password); | |
} catch (error) { | |
console.error(error); | |
this.showToastService.showErrorToast(error.message); | |
} | |
} | |
resetPassword(email: string): Promise<any> { | |
try { | |
return this.afAuth.auth.sendPasswordResetEmail(email); | |
} catch (err) { | |
console.error(err); | |
this.showToastService.showErrorToast(err.message); | |
} | |
} | |
async loginWithFacebook(): Promise<void> { | |
try { | |
const response = await this.facebook.login(['email']); | |
const facebookCredential = firebase.auth.FacebookAuthProvider.credential(response.authResponse.accessToken); | |
const result = await firebase.auth().signInWithCredential(facebookCredential); | |
const userId = result.uid; | |
const userProfileDocRef: AngularFirestoreDocument<UserProfile> = this.fireStore.doc(`userProfiles/${userId}`); | |
const isUserAlreadyExist = await this.isUserAlreadyExist(result.email); | |
if (!isUserAlreadyExist) { | |
const userProfile: UserProfile = { | |
id: userId, | |
email: result.email, | |
name: '', | |
creationTime: firebase.firestore.Timestamp, | |
} | |
await userProfileDocRef.set(userProfile); | |
} | |
} | |
catch (err) { | |
console.log(err); | |
this.showToastService.showErrorToast(err.message); | |
} | |
} | |
async loginWithGoogle(): Promise<void> { | |
try { | |
const response = await this.googlePlus.login({ | |
'webClientId': '', | |
'offline': true | |
}); | |
const result = await firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential(response.idToken)); | |
const userId: string = result.uid; | |
const userProfileDocRef: AngularFirestoreDocument<UserProfile> = this.fireStore.doc(`userProfiles/${userId}`); | |
const isUserAlreadyExist = await this.isUserAlreadyExist(result.email); | |
if (!isUserAlreadyExist) { | |
const userProfile: UserProfile = { | |
id: userId, | |
email: result.email, | |
creationTime: firebase.firestore.Timestamp, | |
name: '', | |
} | |
await userProfileDocRef.set(userProfile); | |
} | |
} | |
catch (err) { | |
console.log(err); | |
this.showToastService.showErrorToast(err.message); | |
} | |
} | |
async isUserAlreadyExist(email: string): Promise<boolean> { | |
try { | |
const res$: Observable<UserProfile[]> = this.fireStore.collection<UserProfile>('userProfiles', ref => ref.where('email', '==', email)).valueChanges(); | |
const result = await res$.pipe(first()).toPromise(); | |
if (result == null) return false; | |
return true; | |
} catch (err) { | |
console.log(err); | |
this.showToastService.showErrorToast(err.message); | |
} | |
} | |
logout(): Promise<void> { | |
try { | |
return this.afAuth.auth.signOut(); | |
} catch (err) { | |
console.error(err); | |
this.showToastService.showErrorToast(err.message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment