gpt4 book ai didi

reactjs - auth.loginWithRedirect() 之后的 Auth0 重定向循环 - @auth0/auth0-spa-js

转载 作者:行者123 更新时间:2023-12-05 07:00:47 27 4
gpt4 key购买 nike

我目前正在将我的(React)应用程序的身份验证从 Auth0 React SDK 切换到普通的 SPA SDK(因为我不喜欢 React SDK 中获取 token 的方式)

我现在面临的问题是,当我登录到应用程序时,我陷入了(我认为)由以下组件引起的无休止的重定向循环。一些上下文:路由器使用 protected 路由,将用户重定向到处理身份验证的/login 路由。在下面的组件中,auth.isAuthenticated() 总是返回 false... 这就是调用 auth.loginWithRedirect() 的原因。但我看不出如何解决这个...

const LoginRoute: React.FunctionComponent<any> = ({ ...props }) => {
const { isLoggedIn, loading } = useSelector(userSelector);
const dispatch = useDispatch();

useEffect(() => {
const handleAsyncLogin = async () => {
const isAuthenticated = await auth.isAuthenticated();
if (isAuthenticated) {
// Auth0 says we are logged in, so we only need to store the user in the store
dispatch(login());
} else if (!!auth && !loading) {
// We are *not* logged in. Go to Auth0 login page.
await auth?.loginWithRedirect();
}
};
handleAsyncLogin();
}, [dispatch, loading]);

return (
<Route
{...props}
component={() => {
if (isLoggedIn) {
console.log("Is Logged In");
// We are *definitely* logged in, go to projects
return <Redirect to="/questionsFromMe" />;
}
if (loading) {
return <Loader />;
}
return <div />;
}}
/>
);
};

请帮帮我!提前致谢。

最佳答案

尝试在 auth0 应用程序属性页面中将“应用程序类型”设置为适合您的应用程序的值(如果您尚未设置)。 enter image description here

关于reactjs - auth.loginWithRedirect() 之后的 Auth0 重定向循环 - @auth0/auth0-spa-js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64030210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com