web/app/account/login/page.tsx
Aidan a76527d839
All checks were successful
Build and Push Docker Image / build_and_push (push) Successful in 1m19s
implement final login flow
2025-01-26 00:07:56 -05:00

94 lines
2.8 KiB
TypeScript

"use client"
import Link from "next/link"
import { useState } from "react"
import { useRouter } from "next/navigation"
import { Mail } from "lucide-react"
import Cookies from "js-cookie"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Alert, AlertDescription } from "@/components/ui/alert"
export default function Login() {
const [email, setEmail] = useState("")
const [errorMessage, setErrorMessage] = useState("")
const [isLoading, setIsLoading] = useState(false)
const router = useRouter()
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setIsLoading(true)
setErrorMessage("")
try {
const response = await fetch('http://localhost:3001/auth/login', {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email }),
})
const data = await response.json()
if (response.ok && data.success) {
Cookies.set("stageTwoKey", data.stageTwoKey)
Cookies.set("email", email)
router.push("/account/login/code")
} else {
setErrorMessage(data.error || "An unknown error occurred.")
}
} catch (error) {
console.error("There was a problem with requesting a magic code:", error)
setErrorMessage("An unexpected error occurred. Please try again.")
} finally {
setIsLoading(false)
}
}
return (
<div className="flex h-screen items-center justify-center">
<Card className="w-full max-w-md">
<CardHeader>
<CardTitle>Log in to your account</CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Input
type="email"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
"Sending..."
) : (
<>
<Mail className="mr-2 h-4 w-4" />
Send Magic Code
</>
)}
</Button>
{errorMessage && (
<Alert variant="destructive">
<AlertDescription>{errorMessage}</AlertDescription>
</Alert>
)}
<div className="text-center">
<Link href="https://user.pontusmail.org/admin/user/signup" className="text-sm underline">
I don&apos;t have an account
</Link>
</div>
</form>
</CardContent>
</Card>
</div>
)
}