1

我在oidc-client带有 Identity Server 4 的 Angular 10 应用程序中使用 npm 包。

我发现当我在浏览器中重新加载网页时,它总是重定向到主页并且不记得当前的角度路线。

我发现这是因为当页面重新加载时UserManager尝试刷新用户对象,但这是异步完成的,因此因为它还没有加载所以AuthGuard看到。nulluser

这个可以修吗。如何?

@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.

4

0 回答 0