Auth0 React SDK 无法识别具有活动会话的返回用户。

huangapple go评论70阅读模式
英文:

Auth0 React SDK not able to identify returning user with active session

问题

My application enduroco.in使用Auth0 React SDK。我面临的问题是,即使我在一个标签上已登录,当我打开一个新标签并转到主页时,它不会让我知道用户已经有一个活动会话。

这是主页组件开头的两行代码。一旦我点击登录,它就完美运行。如果用户有会话,它甚至不会要求输入凭据。

所需行为是isAuthenticated应该为真,而无需点击登录。如果这不可能,那么需要静默获取令牌,如果找到令牌,则导航到仪表板,否则显示主页的<Fragment>

英文:

My application enduroco.in uses Auth0 React SDK. The problem I face is that even if I am logged in on 1 tab when I open a new tab and go the home page it doesn't let me know that the user already has an active session.

These are the two lines of code at the start of home component. It works perfectly once I click on login. If the user has a session it doesn't even ask for credentials.

function Home(props) {
  const { isAuthenticated } = useAuth0();
  if (isAuthenticated) return &lt;Navigate to={&quot;/dashboard&quot;} /&gt;;
  else
    return (
      &lt;Fragment&gt;
        &lt;Hero /&gt;
        &lt;Content /&gt;
      &lt;/Fragment&gt;
    );
}

Desired behavior is that isAuthenticated should be true without having to click on login. If thats not possible then need to silently get tokens and if tokens found navigate to dashboard else show the home &lt;Fragment&gt;

答案1

得分: 0

找到了解决方案。非常简单。

只需在我的 Auth0Provider 属性中设置 useRefreshTokens=truecacheLocation="localstorage"

现在它看起来像这样:

const providerConfig = {
  domain: config.domain,
  clientId: config.clientId,
  ...(config.audience ? { audience: config.audience } : null),
  redirectUri: window.location.origin,
  useRefreshTokens: true,
  cacheLocation: "localstorage",
  onRedirectCallback,
};
英文:

Found the solution. It was very straightforward

Just had to set useRefreshTokens=true and cacheLocation=&quot;localstorage&quot; in my Auth0Provider props

It now looks like this

const providerConfig = {
  domain: config.domain,
  clientId: config.clientId,
  ...(config.audience ? { audience: config.audience } : null),
  redirectUri: window.location.origin,
  useRefreshTokens: true,
  cacheLocation: &quot;localstorage&quot;,
  onRedirectCallback,
};

huangapple
  • 本文由 发表于 2023年4月13日 19:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定