Skip to content

Instantly share code, notes, and snippets.

@ton3s
Created December 9, 2016 09:04
Show Gist options
  • Save ton3s/ba1846c6c243f3aeaead3c38ca6df7b5 to your computer and use it in GitHub Desktop.
Save ton3s/ba1846c6c243f3aeaead3c38ca6df7b5 to your computer and use it in GitHub Desktop.
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