Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import {Injectable} from '@angular/core';
  2. import {FirebaseAuth, AngularFire} from "angularfire2";
  3. import {Subject, Observable, BehaviorSubject} from "rxjs";
  4. import {AuthInfo} from "./auth-info";
  5.  
  6. @Injectable()
  7. export class AuthService {
  8.  
  9. static UNKNOWN_USER = new AuthInfo(null);
  10. authInfo$: BehaviorSubject<AuthInfo> = new BehaviorSubject<AuthInfo>(AuthService.UNKNOWN_USER);
  11.  
  12. constructor(public auth: FirebaseAuth,
  13. public af: AngularFire) {
  14.  
  15. this.af.auth.subscribe(auth => {
  16. if (auth) {
  17. const authInfo = new AuthInfo(auth.uid);
  18. this.authInfo$.next(authInfo);
  19. }
  20. else {
  21. this.authInfo$.next(AuthService.UNKNOWN_USER);
  22. }
  23. });
  24. }
  25.  
  26. login(email: string, password: string) {
  27. return this.fromFirebaseAuthPromise(this.auth.login({email, password}));
  28. }
  29.  
  30. fromFirebaseAuthPromise(promise): Observable<any> {
  31.  
  32. const subject = new Subject<any>();
  33. promise
  34. .then(res => {
  35. subject.next(res);
  36. subject.complete();
  37. },
  38. err => {
  39. subject.error(err);
  40. subject.complete();
  41. });
  42.  
  43. return subject.asObservable();
  44. }
  45.  
  46. logout() {
  47. this.af.auth.logout();
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement