"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { ArrowLeft } from "lucide-react"; import Link from "next/link"; import * as React from "react"; import { useForm } from "react-hook-form"; import * as z from "zod"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot, } from "@/components/ui/input-otp"; import { useAuth } from "@/providers/auth-provider"; const loginSchema = z.object({ email: z.string().email({ message: "Email invalide" }), password: z .string() .min(8, { message: "Le mot de passe doit faire au moins 8 caractères" }), }); type LoginFormValues = z.infer; export default function LoginPage() { const { login, verify2fa } = useAuth(); const [loading, setLoading] = React.useState(false); const [show2fa, setShow2fa] = React.useState(false); const [userId, setUserId] = React.useState(null); const [otpValue, setOtpValue] = React.useState(""); const form = useForm({ resolver: zodResolver(loginSchema), defaultValues: { email: "", password: "", }, }); async function onSubmit(values: LoginFormValues) { setLoading(true); try { const res = await login(values.email, values.password); if (res.userId && res.message === "Please provide 2FA token") { setUserId(res.userId); setShow2fa(true); } } catch (_error) { // Error is handled in useAuth via toast } finally { setLoading(false); } } async function onOtpSubmit(e: React.FormEvent) { e.preventDefault(); if (!userId || otpValue.length !== 6) return; setLoading(true); try { await verify2fa(userId, otpValue); } catch (_error) { // Error handled in useAuth } finally { setLoading(false); } } return (
Retour à l'accueil {show2fa ? "Double Authentification" : "Connexion"} {show2fa ? "Entrez le code à 6 chiffres de votre application d'authentification." : "Entrez vos identifiants pour accéder à votre compte MemeGoat."} {show2fa ? (
setOtpValue(value)} >
) : (
( Email )} /> ( Mot de passe )} /> )}

Vous n'avez pas de compte ?{" "} S'inscrire

); }