unstable

编程入门 行业动态 更新时间:2024-10-05 19:19:14

<a href=https://www.elefans.com/category/jswz/34/1420352.html style=unstable"/>

unstable

你好,我的 nextJs 应用程序有问题我正在使用 next-auth 进行身份验证,当我尝试使用

unstable_getserversession
保护我的页面时,它可以正常工作
localhost
但是当我上传它时在
Vercel
我使用
unstable_getserversession
保护它们的页面打不开,我得到
504: GATEWAY_TIMEOUT
错误

这是受保护页面示例

import React, { useRef, useState } from "react";
import classes from "../../styles/authentication/login.module.scss";
import Image from "next/image";
import Link from "next/link";
// Authentication finctionality
import { signIn } from "next-auth/react";
import { unstable_getServerSession } from "next-auth/next";
import { authOptions } from "../api/auth/[...nextauth]";
import { useRouter } from "next/router";
// Notifications
import { toast } from "react-toastify";
// Spinner
import Spinner from "../../components/layout/Spinner";



function login() {
    // init Router
    const router = useRouter();

    // States
    const [loading, setLoading] = useState(false);

    // References
    const emailRef = useRef();
    const passwordRef = useRef();

    // Functions
    async function handleSubmit(e) {
        e.preventDefault();
        // Values
        const email = emailRef.current.value;
        const password = passwordRef.current.value;

        // Check if Email is Valid email address with regex
        const emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
        if (!emailRegex.test(email)) {
            return toast.error("Please provide a valid email address");
        }

        // Check if Password is at least 6 characters long
        if (password.length < 6) {
            return toast.error("Password must be at least 6 characters long");
        }

        // Set loading to true
        setLoading(true);

        // Sign in
        const result = await signIn("credentials", {
            redirect: false,
            email,
            password,
        });

        // Set loading to false
        setLoading(false);

        if (result.error) {
            toast.error(result.error);
        } else {
            toast.success("You have successfully logged in");
            router.push("/");
        }
    }

    return (
        <div className='container'>
            <div className={classes.Content_Container}>
                <div className={classes.Content}>
                    <section className={classes.Left_Section}>
                        <div className={classes.Image_Container}>
                            <Image
                                src='/Images/Auth/signup_2.png'
                                alt='signup illustration'
                                layout='fill'
                                objectFit='contain'
                            />
                        </div>
                    </section>
                    <section className={classes.Right_Section}>
                        <form onSubmit={handleSubmit}>
                            <h1>Login</h1>
                            <div className={classes.Input_Container}>
                                <input
                                    type='email'
                                    name='email'
                                    id='email'
                                    placeholder=' '
                                    ref={emailRef}
                                />
                                <label htmlFor='email'>Email</label>
                            </div>
                            <div className={classes.Input_Container}>
                                <input
                                    type='password'
                                    name='password'
                                    placeholder=' '
                                    id='password'
                                    ref={passwordRef}
                                />
                                <label htmlFor='password'>Password</label>
                            </div>
                            <div className={classes.Signup_Buttons_Container}>
                                <button
                                    className={classes.Signup_Button}
                                    type='submit'
                                >
                                    {loading ? (
                                        <Spinner size={2} color={`#ffffff`} />
                                    ) : (
                                        `Login`
                                    )}
                                </button>
                                <a
                                    href='/api/auth/signin'
                                    className={classes.Google_Signup}
                                    onClick={(e) => {
                                        e.preventDefault();
                                        signIn("google", {
                                            callbackUrl: "/",
                                        });
                                    }}
                                >
                                    <Image
                                        src='/Images/icons/google.png'
                                        alt='google logo'
                                        width={20}
                                        height={20}
                                    />
                                </a>
                            </div>
                            <Link href='/authentication/signup'>
                                <a>Don't have an account? Register</a>
                            </Link>
                            <Link href='/authentication/resetPassword_p1'>
                                <a>Forgot Password</a>
                            </Link>
                        </form>
                    </section>
                </div>
            </div>
        </div>
    );
}

export default login;

// You should use getServerSideProps when:
// - Only if you need to pre-render a page whose data must be fetched at request time
export const getServerSideProps = async (context) => {
    // // Get session
    const session = await unstable_getServerSession(
        context.req,
        context.res,
        authOptions
    );

    if (session) {
        // If session exists, redirect user to homepage
        return {
            redirect: {
                destination: "/",
                permanent: false,
            },
        };
    }

    return {
        props: {},
    };
};
<script src=".6.3/umd/react.production.min.js"></script>
<script src=".6.3/umd/react-dom.production.min.js"></script>
回答如下:

我发现我在上传项目到

NEXTAUTH_URL
网站之前没有把
.env
文件中的
Vercel
值改成Production Url

更多推荐

unstable

本文发布于:2024-05-30 22:53:00,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:unstable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!