zoukankan      html  css  js  c++  java
  • [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();
    }
    }
  • 相关阅读:
    【NOIP2018】游记
    题解 P1441 【砝码称重】
    题解 P3128 【[USACO15DEC]最大流Max Flow】
    题解 P1949 【聪明的打字员_NOI导刊2011提高(10)】
    题解 P1966 【火柴排队】
    题解 P1895 【数字序列】
    topcoder做题
    1149E
    hdu 6589
    hdu 6579
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6087733.html
Copyright © 2011-2022 走看看