[AngularFire2] Signup and logout
and
2023-09-14 09:00:52 时间
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();
}
}
相关文章
- OpenStack nova VM migration (live and cold) call flow
- [Linux] Install java and add JAVA_HOME, PATH
- [CSS] Create Complex Shapes with CSS Clip Path and Border Radiusc (border-radius & clip-path)
- [React] Validate React Forms with Formik and Yup
- [Poi] Setup PostCSS and Tailwind with Poi
- [AngularFire2 & Firestore] Example for collection and doc
- SAP UI5 binding, property binding and resourceBinding
- 【hdu 1848】Fibonacci again and again
- 成功解决 kerascallbacks.py:999: UserWarning: `epsilon` argument is deprecated and will be removed, use
- configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
- hdu 1028 Ignatius and the Princess III
- mysql 数据表 查找 where and or