首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >supabase auth-helpers-nextjs包不能正常工作

supabase auth-helpers-nextjs包不能正常工作
EN

Stack Overflow用户
提问于 2022-08-16 06:27:45
回答 1查看 642关注 0票数 0

我在我的项目中使用supabase auth助手包。当用户输入登录详细信息时,应该将其重定向到order页面。我在订单页面中使用supabase / auth条件。但是,当用户输入登录详细信息时,它不会被重定向到order页面。

以下是订单页中身份验证的代码:

代码语言:javascript
运行
复制
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 } };
    }
});

登入网页代码如下:

代码语言:javascript
运行
复制
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 >
    )
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-17 13:33:28

在调用router.push之前,需要等待定义用户对象。

代码语言:javascript
运行
复制
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
        })
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73369622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档