我有一个使用
auth0-react
包和 useAuth0
钩子的 React 应用程序。在我使用 Auth0 开发密钥的本地环境中,我可以登录和注销,并且我的会话保持正常。
在我的生产实例中,第一次登录后,我就可以访问用户信息。但是,如果我尝试导航到另一个页面或刷新,用户的会话就会消失,并且 IsAuthenticated 为 false。
我的提供商中有
useRefreshToken={true}
,并且刷新我的 Auth0 仪表板中的令牌处于活动状态。我已经为应用程序和 Google Oauth 配置了 Auth0 配置,将我的生产 URL 作为允许的来源,将个人资料页面作为回调 URI。无法弄清楚为什么会发生这种情况。我正在使用 Chrome
return (
<Auth0Provider
domain={domain}
clientId={clientId}
authorizationParams={{
redirect_uri: redirectUri,
}}
useRefreshTokens
onRedirectCallback={onRedirectCallback}
>
{children}
</Auth0Provider>
);
登录方法
const handleLogin = async () => {
await loginWithRedirect({
appState: {
returnTo: "/profile",
},
});
};
我的导航栏代码在初始登录时显示用户,并在导航后给出未定义、错误
const { user, logout, isLoading, isAuthenticated} = useAuth0();
console.log(user, isLoading);
我尝试更改我的 google ouath 设置,使用或不使用刷新令牌,但对我的生产环境没有任何作用
弄清楚了这一点,问题是我的刷新令牌没有被存储。将
cacheLocation
添加到我的 Auth0Provider 是关键
<Auth0Provider
domain={domain}
clientId={clientId}
authorizationParams={{
redirect_uri: redirectUri,
}}
onRedirectCallback={onRedirectCallback}
cacheLocation="localstorage"
useRefreshTokens={true}
>
{children}
</Auth0Provider>