//Lib import { useForm } from 'react-hook-form'; import { signIn } from 'next-auth/react'; import { useState } from 'react'; import { SpinnerDotted } from 'spinners-react'; import { useRouter } from 'next/router'; import { authOptions } from './api/auth/[...nextauth]'; import { getServerSession } from 'next-auth/next'; //Components import Error from '../Components/UI/Error/Error'; export default function Login() { //Var const { register, handleSubmit, formState: { errors }, reset, } = useForm(); const router = useRouter(); //State const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(); //Functions const formSubmitHandler = async (data) => { setIsLoading(true); setError(null); const resultat = await signIn('credentials', { username: data.username, password: data.password, redirect: false, }); setIsLoading(false); if (resultat.error) { reset(); setError(resultat.error); setTimeout(() => setError(), 4000); } else { router.replace('/'); } }; return (

BorgWarehouse

Sign in to your account.
{error && }

{errors.username && ( {errors.username.message} )}

{errors.password && ( {errors.password.message} )}

); } export async function getServerSideProps(context) { //Var const session = await getServerSession( context.req, context.res, authOptions ); //Here, if I am connected, I redirect to the home page. if (session) { return { redirect: { destination: '/', permanent: false, }, }; } return { props: { session }, }; }