I am writing a guard to re-direct users to the login page if they are unauthenticated using AngularFireAuth.
(如果用户未使用AngularFireAuth进行身份验证,则我正在编写防护措施以将用户重定向到登录页面。)
Without this library, I would store a token in local/session storage and when the guard triggers, I would inspect the storage to see if a token exists. (如果没有此库,我会将令牌存储在本地/会话存储中,并且当防护触发时,我将检查存储以查看令牌是否存在。)
AngularFireAuth stores a token in the database (which contains authentication information): (AngularFireAuth将令牌存储在数据库中(其中包含身份验证信息):)
However it would be unwise to directly inspect this as AngularFireAuth could change how this works at any future date.
(但是,直接进行检查是不明智的,因为AngularFireAuth可能会在将来的任何时间更改其工作方式。)
It is not exactly part of the documented public API. (它不完全是已记录的公共API的一部分。)
In older versions of AngularFireAuth, I could include this method in my service to do this check:
(在较旧版本的AngularFireAuth中,我可以在服务中包含此方法以执行此检查:)
import { AngularFireAuth } from '@angular/fire/auth';
export class AuthService {
constructor(private readonly service: AngularFireAuth) {}
public login(credentials: Credentials) {
const { email, password } = credentials;
return this.service.auth.signInWithEmailAndPassword(email, password);
}
public isLoggedIn(): boolean {
return !!this.service.auth.currentUser;
}
}
However this does not persist on refresh.
(但是,刷新后这种情况不会持续。)
So while I am navigating around the system, it works fine, but if I refresh or leave and come back, I am prompted to log in again. (因此,当我在系统中导航时,它可以正常工作,但是如果刷新或离开然后再返回,系统将提示我再次登录。)
How can I write my isLoggedIn
method to be callable in my guard?
(如何编写可以在守护程序中调用的isLoggedIn
方法?)
ask by nephiw translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…