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

67 lines
2.2 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";
import { AuthForm } from "@/components/chat/auth-form";
import { SubmitButton } from "@/components/chat/submit-button";
import { toast } from "@/components/chat/toast";
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 { update: updateSession } = useSession();
2025-04-25 23:40:15 -07:00
// biome-ignore lint/correctness/useExhaustiveDependencies: router and updateSession 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!" });
2024-11-08 17:59:06 +03:00
setIsSuccessful(true);
updateSession();
router.refresh();
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 (
<>
<h1 className="text-2xl font-semibold tracking-tight">Create account</h1>
<p className="text-sm text-muted-foreground">Get started for free</p>
<AuthForm action={handleSubmit} defaultEmail={email}>
<SubmitButton isSuccessful={isSuccessful}>Sign up</SubmitButton>
<p className="text-center text-[13px] text-muted-foreground">
{"Have an account? "}
<Link
className="text-foreground underline-offset-4 hover:underline"
href="/login"
>
Sign in
</Link>
</p>
</AuthForm>
</>
2024-10-11 18:00:22 +05:30
);
}