initial commit
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
"use client"
|
||||
|
||||
import { SessionLogin } from "@/actions/auth";
|
||||
import Header from "@/components/header";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useForm, SubmitHandler } from "react-hook-form";
|
||||
import { useSearchParams, useRouter } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
|
||||
interface FormData {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export default function LoginPage() {
|
||||
const searchParams = useSearchParams();
|
||||
const router = useRouter();
|
||||
const session = searchParams.get("session");
|
||||
const t = useTranslations("LoginPage");
|
||||
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const { register, handleSubmit, formState: { errors, isSubmitting } } = useForm<FormData>();
|
||||
|
||||
useEffect(() => {
|
||||
if (!session) {
|
||||
window.location.href = "/api/auth/initiate";
|
||||
}
|
||||
}, [session]);
|
||||
|
||||
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
||||
setError(null);
|
||||
|
||||
const result = await SessionLogin(session!, data.email, data.password);
|
||||
|
||||
if (!result.success) {
|
||||
if (result.expired) {
|
||||
window.location.href = "/api/auth/initiate";
|
||||
return;
|
||||
}
|
||||
|
||||
setError(result.message ?? null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.redirectURL) {
|
||||
window.location.href = result.redirectURL;
|
||||
} else {
|
||||
router.push(`/consent?session=${session}`);
|
||||
}
|
||||
};
|
||||
|
||||
if (!session) return (
|
||||
<div className="w-full min-h-svh flex items-center justify-center">
|
||||
<Spinner className="size-6" />
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header />
|
||||
<div className="bg-background flex min-h-svh flex-col items-center justify-center gap-6 p-6 md:p-10">
|
||||
<div className="w-full max-w-sm flex flex-col gap-6">
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<FieldGroup>
|
||||
<div className="flex flex-col">
|
||||
<h1 className="text-xl">{t("title")}</h1>
|
||||
<h1 className="text-xl text-muted-foreground">{t("subtitle")}</h1>
|
||||
</div>
|
||||
|
||||
<Field data-invalid={!!errors.email}>
|
||||
<FieldLabel htmlFor="email">{t("email.title")}</FieldLabel>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.email}
|
||||
{...register("email", {
|
||||
required: t("required"),
|
||||
pattern: { value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: t("email.invalid") },
|
||||
})}
|
||||
/>
|
||||
{errors.email && <FieldError>{errors.email.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.password}>
|
||||
<FieldLabel htmlFor="password">{t("password.title")}</FieldLabel>
|
||||
<Input
|
||||
id="password"
|
||||
type="password"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.password}
|
||||
{...register("password", {
|
||||
required: t("required"),
|
||||
minLength: { value: 8, message: t("password.minLength") },
|
||||
})}
|
||||
/>
|
||||
{errors.password && <FieldError>{errors.password.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<Button type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting && <Spinner />}
|
||||
{t("login")}
|
||||
</Button>
|
||||
{error && <FieldError className="text-center">{error}</FieldError>}
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
<FieldDescription className="px-6 text-center">
|
||||
{t.rich("noAccountNote", {
|
||||
register: (chunks) => <Link href="/register">{chunks}</Link>
|
||||
})}
|
||||
</FieldDescription>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user