码迷,mamicode.com
首页 > 其他好文 > 详细

[AngularFire2] Signup and logout

时间:2016-11-22 07:40:14      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:from   nbsp   login   this   promise   sub   tor   sign   class   


import {AuthProviders, FirebaseAuthState, FirebaseAuth, AuthMethods} from "angularfire2";
import {Injectable} from "@angular/core";
import {Subject, BehaviorSubject} from "rxjs";
import {AuthInfo} from "./AuthInfo";

@Injectable()
export class AuthService {

static UNKNOW_USER = new AuthInfo(null);

private authState: FirebaseAuthState = null;
public authInfo$: BehaviorSubject<AuthInfo> = new BehaviorSubject<AuthInfo>(AuthService.UNKNOW_USER);

constructor(public auth$: FirebaseAuth) {
auth$.subscribe((state: FirebaseAuthState) => {
this.authState = state;
});
}

signUp(email, password){
return this.fromFirebaseAuthPromise(this.auth$.createUser(
{email, password}
));
}

login(email, password) {

return this.fromFirebaseAuthPromise(this.auth$.login({
email, password
},{
method: AuthMethods.Password,
provider: AuthProviders.Password
}));
}

logout(){
this.auth$.logout();
this.authInfo$.next(AuthService.UNKNOW_USER);
}

fromFirebaseAuthPromise(promise) {
const subject = new Subject<any>();

promise.then((res) => {
const uid = this.authState.uid;
const authInfo = new AuthInfo(uid);
this.authInfo$.next(authInfo);
subject.next(res);
subject.complete();
}, err => {
this.authInfo$.error(err);
subject.error(err);
subject.complete();
});

return subject.asObservable();
}
}

 

[AngularFire2] Signup and logout

标签:from   nbsp   login   this   promise   sub   tor   sign   class   

原文地址:http://www.cnblogs.com/Answer1215/p/6087733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!