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

问题描述 投票:0回答:1

我的应用程序 enduroco.in 使用 Auth0 React SDK。我面临的问题是,即使我在打开一个新选项卡并转到主页时登录了 1 个选项卡,它也不会让我知道用户已经有一个活动会话。

这是home组件开头的两行代码。单击登录后,它会完美运行。如果用户有一个会话,它甚至不会要求提供凭据。

function Home(props) {
  const { isAuthenticated } = useAuth0();
  if (isAuthenticated) return <Navigate to={"/dashboard"} />;
  else
    return (
      <Fragment>
        <Hero />
        <Content />
      </Fragment>
    );
}

期望的行为是

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

reactjs authentication auth0
1个回答
0
投票

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

只需在我的 Auth0Provider 道具中设置

useRefreshTokens=true
cacheLocation="localstorage"

现在看起来像这样

const providerConfig = {
  domain: config.domain,
  clientId: config.clientId,
  ...(config.audience ? { audience: config.audience } : null),
  redirectUri: window.location.origin,
  useRefreshTokens: true,
  cacheLocation: "localstorage",
  onRedirectCallback,
};
© www.soinside.com 2019 - 2024. All rights reserved.