|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { AngularFireAuth, AngularFireDatabase, FirebaseAuthState, AuthProviders, AuthMethods, AngularFire } from "angularfire2"; |
| 3 | +// import { User } from "../users/user"; |
| 4 | +import { Router } from "@angular/router"; |
| 5 | + |
| 6 | + |
| 7 | +@Injectable() |
| 8 | +export class AuthService { |
| 9 | + |
| 10 | + public authState: FirebaseAuthState = null; |
| 11 | + |
| 12 | + constructor(private af: AngularFire, |
| 13 | + private db: AngularFireDatabase, |
| 14 | + private router:Router) { |
| 15 | + |
| 16 | + af.auth.subscribe((auth) => { |
| 17 | + this.authState = auth; |
| 18 | + console.log(this.authState) |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + // Returns true if user is logged in |
| 23 | + get authenticated(): boolean { |
| 24 | + return this.authState !== null; |
| 25 | + } |
| 26 | + |
| 27 | + // Returns current user |
| 28 | + get currentUser(): any { |
| 29 | + return this.authenticated ? this.authState.auth : null; |
| 30 | + } |
| 31 | + |
| 32 | + // Returns current user UID |
| 33 | + get currentUserId(): string { |
| 34 | + return this.authenticated ? this.authState.uid : ''; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + //// Social Auth //// |
| 39 | + |
| 40 | + githubLogin(): firebase.Promise<FirebaseAuthState> { |
| 41 | + return this.socialSignIn(AuthProviders.Github); |
| 42 | + } |
| 43 | + |
| 44 | + googleLogin(): firebase.Promise<FirebaseAuthState> { |
| 45 | + return this.socialSignIn(AuthProviders.Google); |
| 46 | + } |
| 47 | + |
| 48 | + facebookLogin(): firebase.Promise<FirebaseAuthState> { |
| 49 | + return this.socialSignIn(AuthProviders.Facebook); |
| 50 | + } |
| 51 | + |
| 52 | + twitterLogin(): firebase.Promise<FirebaseAuthState> { |
| 53 | + return this.socialSignIn(AuthProviders.Twitter); |
| 54 | + } |
| 55 | + |
| 56 | + private socialSignIn(provider: number): firebase.Promise<FirebaseAuthState> { |
| 57 | + return this.af.auth.login({provider, method: AuthMethods.Popup}) |
| 58 | + .then(() => this.writeUserData() ) |
| 59 | + .catch(error => console.log(error)); |
| 60 | + } |
| 61 | + |
| 62 | + //// Email/Password Auth //// |
| 63 | + |
| 64 | + // emailSignUp(email: string, password: string): firebase.Promise<FirebaseAuthState> { |
| 65 | + // return this.af.auth.createUser({ email, password }) |
| 66 | + // .then(() => this.writeUserData()) |
| 67 | + // .catch(error => console.log(error)); |
| 68 | + // } |
| 69 | + // |
| 70 | + // emailLogin(email: string, password: string): firebase.Promise<FirebaseAuthState> { |
| 71 | + // return this.af.auth.login({email, password}) |
| 72 | + // .then(() => this.writeUserData()) |
| 73 | + // .catch(error => console.log(error)); |
| 74 | + // } |
| 75 | + |
| 76 | + //// Anonymous Auth //// |
| 77 | + |
| 78 | + // anonymousLogin() { |
| 79 | + // return this.af.auth.login({ |
| 80 | + // provider: AuthProviders.Anonymous, |
| 81 | + // method: AuthMethods.Anonymous, |
| 82 | + // }) |
| 83 | + // .then(() => this.writeUserData()) |
| 84 | + // .catch(error => console.log(error)); |
| 85 | + // } |
| 86 | + |
| 87 | + //// Sign Out //// |
| 88 | + |
| 89 | + signOut(): void { |
| 90 | + this.af.auth.logout(); |
| 91 | + this.router.navigate(['/']) |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + //// Helpers //// |
| 96 | + |
| 97 | + private writeUserData(): void { |
| 98 | + // Writes user name and email to realtime db |
| 99 | + // useful if your app displays information about users or for admin features |
| 100 | + |
| 101 | + let path = `users/${this.currentUserId}`; // Endpoint on firebase |
| 102 | + let data = { |
| 103 | + name: this.currentUser.displayName, |
| 104 | + email: this.currentUser.email, |
| 105 | + } |
| 106 | + |
| 107 | + this.db.object(path).update(data) |
| 108 | + .catch(error => console.log(error)); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +} |
0 commit comments