diff --git a/src/app/(auth)/login/page.tsx b/src/app/(auth)/login/page.tsx index f7ecaea..e89ffda 100644 --- a/src/app/(auth)/login/page.tsx +++ b/src/app/(auth)/login/page.tsx @@ -2,8 +2,48 @@ import Form from "#/components/form/form" import { loginSchema } from "#/schema/loginSchema" +import { useMutation } from "@tanstack/react-query" +import { signIn } from "next-auth/react" +import { useRouter } from "next/navigation"; export default function LoginPage() { + const router = useRouter() + + const mutation = useMutation({ + mutationKey: ['login'], + mutationFn: async (data: { email: string; password: string }) => { + try { + const result = await signIn("credentials", { + email: data.email, + password: data.password, + redirect: false, + }) + + if (result?.error) { + const errorMessage = result.error.includes("CredentialsSignin") + ? "Email ou mot de passe incorrect" + : result.error; + console.error(errorMessage) + throw new Error(result.error) + } + return result + } catch (error: any) { + if (error.message.includes("Network Error")) { + console.error("Problème de connexion au serveur"); + } + console.error("Autre = ", error); + } + + + }, + onSuccess: () => { + router.push('/admin/home') + }, + onError: (error: Error) => { + console.error(error.message) + }, + }) + return(