chatbot-template/app/(auth)/login/page.tsx

78 lines
2.5 KiB
TypeScript
Raw Normal View History

"use client";
2024-10-11 18:00:22 +05:30
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import { useActionState, useEffect, useState } from "react";
2024-10-11 18:00:22 +05:30
import { AuthForm } from "@/components/auth-form";
import { SubmitButton } from "@/components/submit-button";
import { toast } from "@/components/toast";
import { type LoginActionState, login } from "../actions";
2024-10-11 18:00:22 +05:30
export default function Page() {
const router = useRouter();
const [email, setEmail] = useState("");
2024-11-08 17:59:06 +03:00
const [isSuccessful, setIsSuccessful] = useState(false);
2024-10-11 18:00:22 +05:30
const [state, formAction] = useActionState<LoginActionState, FormData>(
login,
{
status: "idle",
}
2024-10-11 18:00:22 +05:30
);
2025-04-25 23:40:15 -07:00
const { update: updateSession } = useSession();
2024-10-11 18:00:22 +05:30
useEffect(() => {
if (state.status === "failed") {
2025-03-04 17:25:46 -08:00
toast({
type: "error",
description: "Invalid credentials!",
2025-03-04 17:25:46 -08:00
});
} else if (state.status === "invalid_data") {
2025-03-04 17:25:46 -08:00
toast({
type: "error",
description: "Failed validating your submission!",
2025-03-04 17:25:46 -08:00
});
} else if (state.status === "success") {
2024-11-08 17:59:06 +03:00
setIsSuccessful(true);
2025-04-25 23:40:15 -07:00
updateSession();
2024-10-11 18:00:22 +05:30
router.refresh();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.status, router.refresh, updateSession]);
2024-10-11 18:00:22 +05:30
const handleSubmit = (formData: FormData) => {
setEmail(formData.get("email") as string);
2024-10-11 18:00:22 +05:30
formAction(formData);
};
return (
2025-09-09 15:44:07 -04:00
<div className="flex h-dvh w-screen items-start justify-center bg-background pt-12 md:items-center md:pt-0">
<div className="flex w-full max-w-md flex-col gap-12 overflow-hidden rounded-2xl">
2024-10-11 18:00:22 +05:30
<div className="flex flex-col items-center justify-center gap-2 px-4 text-center sm:px-16">
2025-09-09 15:44:07 -04:00
<h3 className="font-semibold text-xl dark:text-zinc-50">Sign In</h3>
<p className="text-gray-500 text-sm dark:text-zinc-400">
2024-10-11 18:00:22 +05:30
Use your email and password to sign in
</p>
</div>
<AuthForm action={handleSubmit} defaultEmail={email}>
2024-11-08 17:59:06 +03:00
<SubmitButton isSuccessful={isSuccessful}>Sign in</SubmitButton>
2025-09-09 15:44:07 -04:00
<p className="mt-4 text-center text-gray-600 text-sm dark:text-zinc-400">
2024-10-11 18:00:22 +05:30
{"Don't have an account? "}
<Link
className="font-semibold text-gray-800 hover:underline dark:text-zinc-200"
href="/register"
2024-10-11 18:00:22 +05:30
>
Sign up
</Link>
{" for free."}
2024-10-11 18:00:22 +05:30
</p>
</AuthForm>
</div>
</div>
);
}