Skip to content

Instantly share code, notes, and snippets.

@GeoffMahugu
Created January 20, 2019 05:17
Show Gist options
  • Save GeoffMahugu/764d9d2bedc1720042fcf8ae5b155054 to your computer and use it in GitHub Desktop.
Save GeoffMahugu/764d9d2bedc1720042fcf8ae5b155054 to your computer and use it in GitHub Desktop.
Angular Fire Auth Service
import { Injectable } from "@angular/core";
import { Router } from "@angular/router";
import * as firebase from "firebase/app";
import { AngularFireAuth } from "angularfire2/auth";
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/switchMap";
import "rxjs/add/operator/catch";
import "rxjs/observable/of";
import { MatSnackBar } from "@angular/material";
import { environment } from "../../environments/environment";
interface User {
uid: string;
email: string;
}
@Injectable()
export class AuthService {
user: Observable<User>;
constructor(
public afAuth: AngularFireAuth,
private router: Router,
private snackBar: MatSnackBar
) {
this.user = this.afAuth.authState.switchMap(user => {
if (user) {
this.updateUserData(user);
}
return Observable.of(user);
});
}
googleLogin() {
const provider = new firebase.auth.GoogleAuthProvider();
return this.oAuthLogin(provider)
.then(user => {
this.snackBar.open("Successfully Signed In with Google.", "CLOSE", {
duration: 3500
});
this.updateUserData(user);
})
.catch(error => this.handleError(error));
}
facebookLogin() {
const provider = new firebase.auth.FacebookAuthProvider();
return this.oAuthLogin(provider)
.then(user => {
this.snackBar.open("Successfully Signed In with Facebook.", "CLOSE", {
duration: 3500
});
this.updateUserData(user);
})
.catch(error => this.handleError(error));
}
public oAuthLogin(provider) {
return this.afAuth.auth
.signInWithPopup(provider)
.then(credential => {
this.updateUserData(credential.user);
})
.catch(error => this.handleError(error));
}
//// Email/Password Auth ////
emailSignUp(email: string, password: string) {
return this.afAuth.auth
.createUserWithEmailAndPassword(email, password)
.then(user => {
this.updateUserData(user); // if using firestore
})
.catch(error => this.handleError(error));
}
emailLogin(email: string, password: string) {
return this.afAuth.auth
.signInWithEmailAndPassword(email, password)
.then(user => {
this.updateUserData(user); // if using firestore
})
.catch(error => this.handleError(error));
}
public updateUserData(user) {
const data: User = {
uid: user.uid,
email: user.email
};
this.social_user = JSON.stringify(user);
localStorage.setItem(
"firebase:authUser:AIzaSySQ97Sh8932MjHh0e00Nsau_hrHG5Bvas:my-app-name",
);
return user;
}
signOut() {
return this.afAuth.auth.signOut().then(() => {
localStorage.clear();
this.router.navigate(["/"]);
this.snackBar.open("You have been Logged Out.", "CLOSE", {
duration: 3500
});
});
}
// If error, console log and notify user
private handleError(error) {
if (error.code === "auth/popup-blocked") {
this.snackBar.open(
"Your browser has disabled Popups. Please Try again",
"CLOSE"
);
} else if (error.code === "auth/popup-closed-by-user") {
this.snackBar.open("Please reload and try again.", "CLOSE", {
duration: 3000
});
} else {
this.snackBar.open(error.message, "CLOSE", { duration: 3500 });
}
return error.message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment