angular - Angular LoggedIn 方法
问题描述
如何添加loginIn 方法AuthService
以便获得关于用户状态的布尔值?我想要它的CanActivate
方法和AuthService
看起来像这样:
login(email: string, password: string) {
return this.http.post<AuthResponseData>(
'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=',
{
email,
password,
returnSecureToken: true
}
)
.pipe(tap(resData => {
this.handleAuthentication(
resData.email,
resData.localId,
resData.idToken,
+resData.expiresIn);
})
);
}
// tslint:disable-next-line:typedef
private handleAuthentication(
email: string,
userId: string,
token: string,
expiresIn: number) {
const expirationDate = new Date(new Date().getTime() + expiresIn * 1000);
const user = new User(
email,
userId,
token,
expirationDate
);
this.user.next(user);
}
解决方案
登录成功后,您可以将 auth 服务中的变量设置为 true 并在注销时设置为 false 然后在服务中创建一个方法作为 getauthstatus,一旦您在 canActivate 构造函数中注入 authservice 类,您就可以访问该方法或更精确的是不要创建额外的变量,只需检查您在 getloggedin 状态下创建的用户对象的可用性,并且可以将其注入到 canActivate 构造函数中
推荐阅读
- python - 如何对数组中的特定元素求和
- c# - asp .net核心中的自动控制器检测
- vb.net - 需要在for循环中添加延迟
- excel - 导入数据时出现部门问题
- ios - 如何支持长时间运行的 cloudkit 同步
- python - 将正则表达式应用于 pandas 数据框列
- c - 如何在 slstatus 中设置 vol_perc(一个无趣的工具)?
- node.js - 如何将选定复选框的数组数据发送到node.js中的模型
- charts - Google Chart 不接受我的数据 - 声明列可能不是字符串
- python - Python中的线程和非线程Win10Toast通知有什么区别?