Skip to content

Instantly share code, notes, and snippets.

@menuka94
Forked from ton3s/auth-service.ts
Created May 17, 2017 11:20
Show Gist options
  • Save menuka94/d21187e682a106875fbbac1c0bb45200 to your computer and use it in GitHub Desktop.
Save menuka94/d21187e682a106875fbbac1c0bb45200 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