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

80 lines
2.7 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 { useActionState, useEffect, useState } from "react";
import { AuthForm } from "@/components/auth-form";
import { SubmitButton } from "@/components/submit-button";
import { toast } from "@/components/toast";
import { useSession } from "@/lib/client";
import { type RegisterActionState, register } 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<RegisterActionState, FormData>(
register,
{
status: "idle",
}
2024-10-11 18:00:22 +05:30
);
const { refetch } = useSession();
2025-04-25 23:40:15 -07:00
// biome-ignore lint/correctness/useExhaustiveDependencies: router and refetch are stable refs
2024-10-11 18:00:22 +05:30
useEffect(() => {
if (state.status === "user_exists") {
toast({ type: "error", description: "Account already exists!" });
} else if (state.status === "failed") {
toast({ type: "error", description: "Failed to create account!" });
} 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") {
toast({ type: "success", description: "Account created successfully!" });
2025-03-04 17:25:46 -08:00
2024-11-08 17:59:06 +03:00
setIsSuccessful(true);
refetch();
router.push("/");
2024-10-11 18:00:22 +05:30
}
2025-10-09 18:49:46 +01:00
}, [state.status]);
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">
<h3 className="font-semibold text-xl dark:text-neutral-50">
Sign Up
</h3>
<p className="text-gray-500 text-sm dark:text-neutral-400">
2024-10-11 18:00:22 +05:30
Create an account with your email and password
</p>
</div>
<AuthForm action={handleSubmit} defaultEmail={email}>
2024-11-08 17:59:06 +03:00
<SubmitButton isSuccessful={isSuccessful}>Sign Up</SubmitButton>
<p className="mt-4 text-center text-gray-600 text-sm dark:text-neutral-400">
{"Already have an account? "}
2024-10-11 18:00:22 +05:30
<Link
className="font-semibold text-gray-800 hover:underline dark:text-neutral-200"
href="/login"
2024-10-11 18:00:22 +05:30
>
Sign in
</Link>
{" instead."}
2024-10-11 18:00:22 +05:30
</p>
</AuthForm>
</div>
</div>
);
}