Created
December 9, 2016 09:04
-
-
Save ton3s/ba1846c6c243f3aeaead3c38ca6df7b5 to your computer and use it in GitHub Desktop.
This file contains 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 {FirebaseAuth, AngularFire} from "angularfire2"; | |
import {Subject, Observable, BehaviorSubject} from "rxjs"; | |
import {AuthInfo} from "./auth-info"; | |
@Injectable() | |
export class AuthService { | |
static UNKNOWN_USER = new AuthInfo(null); | |
authInfo$: BehaviorSubject<AuthInfo> = new BehaviorSubject<AuthInfo>(AuthService.UNKNOWN_USER); | |
constructor(public auth: FirebaseAuth, | |
public af: AngularFire) { | |
this.af.auth.subscribe(auth => { | |
if (auth) { | |
const authInfo = new AuthInfo(auth.uid); | |
this.authInfo$.next(authInfo); | |
} | |
else { | |
this.authInfo$.next(AuthService.UNKNOWN_USER); | |
} | |
}); | |
} | |
login(email: string, password: string) { | |
return this.fromFirebaseAuthPromise(this.auth.login({email, password})); | |
} | |
fromFirebaseAuthPromise(promise): Observable<any> { | |
const subject = new Subject<any>(); | |
promise | |
.then(res => { | |
subject.next(res); | |
subject.complete(); | |
}, | |
err => { | |
subject.error(err); | |
subject.complete(); | |
}); | |
return subject.asObservable(); | |
} | |
logout() { | |
this.af.auth.logout(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment