我在oidc-client
带有 Identity Server 4 的 Angular 10 应用程序中使用 npm 包。
我发现当我在浏览器中重新加载网页时,它总是重定向到主页并且不记得当前的角度路线。
我发现这是因为当页面重新加载时UserManager
尝试刷新用户对象,但这是异步完成的,因此因为它还没有加载所以AuthGuard
看到。null
user
这个可以修吗。如何?
@Injectable({
providedIn: 'root'
})
export class AuthService {
private manager = new UserManager(getClientSettings(this.appSettingsService));
private user: User | null;
constructor(private appSettingsService: AppSettingsService) {
// This should wait until the user has acually loaded. Doing async just makes everything break.
this.manager.getUser().then(user => {
this.user = user;
});
}
login() {
return this.manager.signinRedirect();
}
async completeAuthentication() {
this.user = await this.manager.signinRedirectCallback();
}
isAuthenticated(): boolean {
// This needs to be made to wait until the user has loaded.
return this.user != null && !this.user.expired;
}
似乎无法更改isAuthenticated(): boolean
为,isAuthenticated(): Observable<boolean>
因为它需要在AuthGuard
必须canActivate
返回的地方boolean
而不是必须返回的地方Observable<boolean>
,因此有必要摆脱Observable
.