首页 > 代码库 > [AngularFire2] Signup and logout
[AngularFire2] Signup and logout
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。