我在我的项目中使用supabase auth助手包。当用户输入登录详细信息时,应该将其重定向到order页面。我在订单页面中使用supabase / auth条件。但是,当用户输入登录详细信息时,它不会被重定向到order页面。
以下是订单页中身份验证的代码:
export const getServerSideProps = withPageAuth({ redirectTo: '/admin/login',
async getServerSideProps(ctx) {
// Access the user object
const { user, accessToken } = await getUser(ctx);
return { props: { email: user?.email } };
}
});
登入网页代码如下:
async function handleSubmit(e) {
e.preventDefault();
const { user, error } = await supabase.auth.signIn({
email,
password
})
router.push('/orders')
}
return (
<Layout>
<div className="flex items-center justify-center h-screen">
<form onSubmit={handleSubmit}>
<p className="text-center text-[27px] text-white">Admin Login</p>
<div className="mt-4 text-white">
<p className="text-[14px]">Email</p>
<input type="text" className="bg-[#4A4949] rounded-md py-2 px-1 w-[300px]"
placeholder="Email Address"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<div className="mt-4 text-white">
<p className="text-[14px]">Password</p>
<input type="password" className="bg-[#4A4949] rounded-md py-2 px-1 w-[300px]"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<button className="mt-8 bg-[#4A4949] text-white rounded w-[300px] py-2">Login</button>
</form>
</div>
</Layout >
)
}
发布于 2022-08-17 13:33:28
在调用router.push
之前,需要等待定义用户对象。
import { useUser } from '@supabase/supabase-auth-helpers/react';
const { user } = useUser();
useEffect(() => {
if (user) {
router.push('/orders');
}
}, [user]);
async function handleSubmit(e) {
e.preventDefault();
const { user, error } = await supabase.auth.signIn({
email,
password
})
}
https://stackoverflow.com/questions/73369622
复制相似问题