首页 > 解决方案 > auth0 parseHash 无法在哈希字符串上创建属性“__enableIdPInitiatedLogin”

问题描述

我正在尝试将我的 React Web 应用程序从 auth0-js 9.6.1 升级到 9.7.3。安装新库后,我的 Slack 登录流程不再起作用,它似乎在回调中中断。

TypeError: Cannot create property '__enableIdPInitiatedLogin' on string '#access_token={token string}&token_type=Bearer&state={state string}'

我的parseHash电话是:

this.auth0.parseHash(hash, (err, authResult) => {
  if (authResult && authResult.idToken) {
    AuthService.setToken(authResult.idToken); // JWT returned from Auth0;

    // Redirect user to content.
    const returnUrl = localStorage.getItem(Variables.RETURN_URL_KEY);
    localStorage.removeItem(Variables.RETURN_URL_KEY);
    returnUrl
        ? window.location.replace(returnUrl)
        : window.location.replace("/");
  } else if (err) {
    console.log("Error with auth callback", err);
    window.location.replace("https://foo.com"); // If auth fails, send user to home page.
  }
}

这在 9.6.1 中工作正常,但在 9.7.x 中失败,我找不到任何会导致它开始失败的重大更改。有任何想法吗?

标签: reactjsauth0

解决方案


我和你有同样的问题,所以我在 Auth0.js 库 github 页面上开了一张票。

这是我从开发人员那里得到的回复:

考虑到我们希望第一个参数是对象或回调函数,它当时是偶然工作的(在你的情况下,字符串也被忽略了)。

我们所有的文档都提到:

https://github.com/auth0/auth0.js#api

https://auth0.github.io/auth0.js/global.html#parseHash

https://auth0.com/docs/libraries/auth0js/v9#extract-the-authresult-and-get-user-info

在您的情况下,最简单的解决方法是仅删除第一个参数并仅保留 callback。当没有选项对象时,已经使用了 window.location.hash。

(强调修复我的)

我用 9.7.3 测试了this.auth.auth0.parseHash((err, result) => ...它,它就像一个魅力。

我希望这会有所帮助!


推荐阅读