首页 > 解决方案 > Angular 7 - Auth0 - parseHash 响应为空

问题描述

Auth0 问题。

我的应用程序在浏览器刷新时注销并且用户配置文件出现问题后重新触发登录事件。我将其追踪到身份验证服务中的 parseHash :

  this.auth0.parseHash({ hash: window.location.hash }, (err, authResult) => {
  ...
  }

使用 ngrx 效果触发:

  @Effect()
  init$ = defer(() => {
    const userData = localStorage.getItem("user");

    if (userData != null && userData != 'undefined') {
       let authActionObservable = of(new Login({user: JSON.parse(userData)}));
       this.authService.handleAuthentication();
       return authActionObservable;
    }
    this.authService.handleAuthentication();
  });

似乎 this.auth0.parseHash 在页面刷新后为 authResult 和 err 返回 null 但在初始登录时 authResult 被正确填充。

从技术上讲,登录是成功的,我得到了令牌。我检查了整个配置、域等,一切似乎都很好。我还尝试使用 { hash: window.location.hash }。

标签: angulartypescriptauthenticationauth0

解决方案


问题在于,包含服务令牌的内存变量的状态在每次页面刷新时都会重置。一旦我重新初始化了服务构造函数中的值,问题就消失了。

这里以 localStorage 为例说明我的意思:

  constructor(public router: Router, private store: Store<State>, private http: HttpClient) {
    this._idToken = localStorage.getItem("Auth0IdToken");
    this._accessToken = localStorage.getItem("Auth0AccessToken");
    this._expiresAt = parseInt(localStorage.getItem("Auth0ExpiresAt"));
  }

推荐阅读