AngularFire2 - 如何在页面刷新后保持登录状态
AngularFire2 - How to maintain logged in state after page refresh
我正在为应用程序使用 AngularFire2,我已经获得了与 Firebase 一起使用的 registration/login 功能,但是,每次刷新页面时,登录状态都会重置并且不会持续存在。我无法找到执行此操作的功能,但我觉得我遗漏了一些非常小的东西。
我可以使用 AngularFireAuth
检查某处的页面加载吗?
这是我的授权提供商代码:
import { Injectable } from '@angular/core';
import {Observable, Subject, BehaviorSubject} from "rxjs/Rx";
import {AngularFireAuth, FirebaseAuthState} from "angularfire2";
import {AuthInfo} from "./auth-info";
import {Router} from "@angular/router";
@Injectable()
export class AuthService {
static UNKNOWN_USER = new AuthInfo(null);
authInfo$: BehaviorSubject<AuthInfo> = new BehaviorSubject<AuthInfo>(AuthService.UNKNOWN_USER);
constructor(private auth: AngularFireAuth, private router:Router) {
}
login(email, password):Observable<FirebaseAuthState> {
return this.fromFirebaseAuthPromise(this.auth.login({email, password}));
}
signUp(email, password) {
return this.fromFirebaseAuthPromise(this.auth.createUser({email, password}));
}
fromFirebaseAuthPromise(promise):Observable<any> {
const subject = new Subject<any>();
promise
.then(res => {
const authInfo = new AuthInfo(this.auth.getAuth().uid);
this.authInfo$.next(authInfo);
subject.next(res);
subject.complete();
},
err => {
this.authInfo$.error(err);
subject.error(err);
subject.complete();
});
return subject.asObservable();
}
logout() {
this.auth.logout();
this.authInfo$.next(AuthService.UNKNOWN_USER);
this.router.navigate(['/login']);
}
}
提前致谢!
AngularFireAuth
是一个可观察对象并发出 FirebaseAuthState
值。如果用户登录并刷新页面,AngularFireAuth
将发出经过身份验证的 FirebaseAuthState
;否则,它将发出 null
.
所以像这样的事情应该接近解决你的问题:
constructor(private auth: AngularFireAuth, private router:Router) {
auth.subscribe((authState) => {
if (authState) {
const authInfo = new AuthInfo(authState.uid);
this.authInfo$.next(authInfo);
}
});
}
我正在为应用程序使用 AngularFire2,我已经获得了与 Firebase 一起使用的 registration/login 功能,但是,每次刷新页面时,登录状态都会重置并且不会持续存在。我无法找到执行此操作的功能,但我觉得我遗漏了一些非常小的东西。
我可以使用 AngularFireAuth
检查某处的页面加载吗?
这是我的授权提供商代码:
import { Injectable } from '@angular/core';
import {Observable, Subject, BehaviorSubject} from "rxjs/Rx";
import {AngularFireAuth, FirebaseAuthState} from "angularfire2";
import {AuthInfo} from "./auth-info";
import {Router} from "@angular/router";
@Injectable()
export class AuthService {
static UNKNOWN_USER = new AuthInfo(null);
authInfo$: BehaviorSubject<AuthInfo> = new BehaviorSubject<AuthInfo>(AuthService.UNKNOWN_USER);
constructor(private auth: AngularFireAuth, private router:Router) {
}
login(email, password):Observable<FirebaseAuthState> {
return this.fromFirebaseAuthPromise(this.auth.login({email, password}));
}
signUp(email, password) {
return this.fromFirebaseAuthPromise(this.auth.createUser({email, password}));
}
fromFirebaseAuthPromise(promise):Observable<any> {
const subject = new Subject<any>();
promise
.then(res => {
const authInfo = new AuthInfo(this.auth.getAuth().uid);
this.authInfo$.next(authInfo);
subject.next(res);
subject.complete();
},
err => {
this.authInfo$.error(err);
subject.error(err);
subject.complete();
});
return subject.asObservable();
}
logout() {
this.auth.logout();
this.authInfo$.next(AuthService.UNKNOWN_USER);
this.router.navigate(['/login']);
}
}
提前致谢!
AngularFireAuth
是一个可观察对象并发出 FirebaseAuthState
值。如果用户登录并刷新页面,AngularFireAuth
将发出经过身份验证的 FirebaseAuthState
;否则,它将发出 null
.
所以像这样的事情应该接近解决你的问题:
constructor(private auth: AngularFireAuth, private router:Router) {
auth.subscribe((authState) => {
if (authState) {
const authInfo = new AuthInfo(authState.uid);
this.authInfo$.next(authInfo);
}
});
}