【发布时间】:2019-11-10 16:56:15
【问题描述】:
目标:我正在尝试使用 IdentityServer4 和 oidc 客户端在 Angular 应用程序中隐藏和显示登录和注销按钮。
问题:我的 isLoggedIn 函数的响应在返回 true 之前返回 undifined,并且 *ngIf 从不显示注销按钮或隐藏登录按钮。见代码:
app.componenet.html:
<span>
<button *ngIf="!isLoggedIn()" mat-button (click)="login()">Login</button>
<button *ngIf="isLoggedIn()" mat-button (click)="logout()">Logout</button>
</span>
app.componenet.ts:
isLoggedIn(){
console.log('isLoggedIn -this._authService.isLoggedIn():',
this._authService.isLoggedIn());
this._authService.isLoggedIn();
}
auth.service.ts
isLoggedIn(): boolean{
return this._user && this._user.access_token && !this._user.expired;
}
在 auth.service.ts 我这样设置用户对象:
this._userManager = new UserManager(config);
this._userManager.getUser().then(user =>{
if(user && !user.expired){
this._user = user;
}
我尝试过的:
- 我尝试使用我的 oidc-client 版本,切换 在最低版本和 1.4.1 之间,并确保它们匹配 package.json 和我的 oidc-login-redirect.html 文件。
- 将 auth.service.ts isLoggedIn 函数转换为 promise isLoggedIn() 并使用异步管道直接从 *nfIf 调用它
auth.service.ts 承诺{ 返回新的承诺(解决 => { 解决(this._user && this._user.access_token && !this._user.expired); }); }
app.component.html
<button *ngIf="!this._authService.isLoggedIn() | async" mat-button (click)="login()">Login</button>
<button *ngIf="this._authService.isLoggedIn() | async" mat-button (click)="logout()">Logout</button>
【问题讨论】:
-
我认为您没有包含足够的代码来解决这个问题。没有理由必须将您的代码包装在一个 Promise 中或使用
async。由于设置了this._user,因此必须发生延迟。这是有道理的,因为在 oidc_client 中你必须调用manager.getUser()来返回一个承诺。所以有多种状态,unknown、loggedin、loggedout。请务必考虑到这一点。 -
@DanielGimenez 我添加了设置 this._user 的代码,如果您对缺少的内容有任何建议,请告诉我。
-
我希望
authService持有一个 Behavior Subject 等,并在用户登录时发出一个值。这将使您的组件中的所有内容都更加清晰,其中设置了局部变量在ngOnInit上,您的模板将仅基于该局部变量
标签: javascript angular identityserver4 openid-connect