英文:
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 <Navigate to={"/dashboard"} />;
else
return (
<Fragment>
<Hero />
<Content />
</Fragment>
);
}
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 <Fragment>
答案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,
};
英文:
Found the solution. It was very straightforward
Just had to set useRefreshTokens=true
and cacheLocation="localstorage"
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: "localstorage",
onRedirectCallback,
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论