【问题标题】:ComponentDidMount getting triggered before redirect callbackComponentDidMount 在重定向回调之前被触发
【发布时间】:2020-08-17 06:10:28
【问题描述】:

我正在使用 React 框架。 当我使用 loginRedirect() 方法登录时,我被重定向回我提供的重定向 uri - https:localhost:3000

在调用componentDidMount() 时重定向(返回我的页面)后,msalInstance.getAccount() 返回 null,因为尚未创建令牌。 在此之后触发重定向回调。

componentDidMount() 在触发重定向回调和创建令牌之前被调用。 因此,我必须在重定向后重新加载页面,以便 msalInstance.getAccount() 出现在 componentDidMount() 中。 我该如何处理这种情况?

【问题讨论】:

    标签: javascript reactjs redirect oauth msal


    【解决方案1】:

    问题在于 @azure/msal-browser 的 v2.0.0-beta。 随后在下一个版本中修复-v2.0.0-beta.1

    【讨论】:

      【解决方案2】:

      因为你没有等待你的回应,所以你得到了空值。尝试在 componentDidMount() 中使用 Promises 或 async/await

      【讨论】:

      • 我正在使用 async/await,令牌是在页面加载后创建的。重定向发生时令牌不存在,因此const account = msalInstance.getAccount() 返回null
      • 您不等待您的回复。试试 const account = await msalInstance.getAccount()
      • 它不返回承诺。
      猜你喜欢
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多