"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { Key } from "lucide-react" import Cookies from "js-cookie" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Alert, AlertDescription } from "@/components/ui/alert" export default function Login() { const [magicCode, setMagicCode] = useState("") const [errorMessage, setErrorMessage] = useState("") const [isLoading, setIsLoading] = useState(false) const router = useRouter() useEffect(() => { const validateStageTwo = async () => { setErrorMessage("") setIsLoading(true) try { const response = await fetch('http://localhost:3001/auth/validateStageTwo', { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: Cookies.get("email"), stageTwoKey: Cookies.get("stageTwoKey") }), }) const data = await response.json() if (!response.ok || !data.success) { router.push("/account/login") } } catch (error) { console.error("There was a problem with checking the status of your request:", error) setErrorMessage("An unexpected error occurred. Please try again.") } finally { setIsLoading(false) } } validateStageTwo() }, [magicCode, router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setErrorMessage("") try { const response = await fetch('http://localhost:3001/auth/validateMagicCode', { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: Cookies.get("email"), stageTwoKey: Cookies.get("stageTwoKey"), magicCode }), }) const data = await response.json() if (response.ok && data.success) { Cookies.set("key", data.key) Cookies.remove("stageTwoKey") router.push("/account/dashboard") } else { setErrorMessage(data.error || "An unknown error occurred.") } } catch (error) { console.error("There was a problem with checking the magic code:", error) setErrorMessage("An unexpected error occurred. Please try again.") } finally { setIsLoading(false) } } return (