Guest User

Untitled

a guest
Jan 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { AngularFireAuth } from 'angularfire2/auth';
  4. import * as firebase from 'firebase/app';
  5. import { Observable } from 'rxjs/Observable';
  6. import 'rxjs/add/operator/switchMap';
  7.  
  8. @Injectable()
  9. export class AuthService {
  10. private LANDING_PAGE = '/profile';
  11. public currentUser: firebase.User;
  12.  
  13. constructor(
  14. public afAuth: AngularFireAuth,
  15. private router: Router) {
  16. this.afAuth.auth.onAuthStateChanged((user) => {
  17. this.currentUser = user;
  18. });
  19. }
  20.  
  21. login(email: string, password: string) {
  22. this.afAuth.auth.signInWithEmailAndPassword(email, password)
  23. .then(value => {
  24. this.router.navigateByUrl(this.LANDING_PAGE);
  25. })
  26. .catch(err => {
  27. });
  28. }
  29.  
  30. emailSignup(email: string, password: string) {
  31. this.afAuth.auth.createUserWithEmailAndPassword(email, password)
  32. .then(value => {
  33.  
  34. this.router.navigateByUrl(this.LANDING_PAGE);
  35. })
  36. .catch(error => {
  37. });
  38. }
  39.  
  40. googleLogin() {
  41. const provider = new firebase.auth.GoogleAuthProvider();
  42. return this.oAuthLogin(provider)
  43. .then(value => {
  44.  
  45. this.router.navigateByUrl(this.LANDING_PAGE);
  46. })
  47. .catch(error => {
  48. });
  49. }
  50.  
  51. logout() {
  52. this.afAuth.auth.signOut().then(() => {
  53.  
  54. this.router.navigate(['/']);
  55. });
  56. }
  57.  
  58. private oAuthLogin(provider) {
  59. return this.afAuth.auth.signInWithPopup(provider);
  60. }
  61. }
Add Comment
Please, Sign In to add comment