"use client"; import { AnimatePresence, motion } from "framer-motion"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { Logo } from "@/components/Logo"; import { getAuthErrorDetails, isNetworkError, shouldRetry } from "@/lib/auth-errors"; import { AmbientBackground } from "../login/AmbientBackground"; export default function RegisterPage() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [error, setError] = useState(null); const [errorTitle, setErrorTitle] = useState(null); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); // Check authentication type and redirect if not LOCAL useEffect(() => { const authType = process.env.NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE || "GOOGLE"; if (authType !== "LOCAL") { router.push("/login"); } }, [router]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); // Form validation if (password !== confirmPassword) { setError("Passwords do not match"); setErrorTitle("Password Mismatch"); toast.error("Password Mismatch", { description: "The passwords you entered do not match", duration: 4000, }); return; } setIsLoading(true); setError(null); // Clear any previous errors setErrorTitle(null); // Show loading toast const loadingToast = toast.loading("Creating your account..."); try { const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/auth/register`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email, password, is_active: true, is_superuser: false, is_verified: false, }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.detail || `HTTP ${response.status}`); } // Success toast toast.success("Account created successfully!", { id: loadingToast, description: "Redirecting to login page...", duration: 2000, }); // Small delay to show success message setTimeout(() => { router.push("/login?registered=true"); }, 500); } catch (err) { // Use auth-errors utility to get proper error details let errorCode = "UNKNOWN_ERROR"; if (err instanceof Error) { errorCode = err.message; } else if (isNetworkError(err)) { errorCode = "NETWORK_ERROR"; } // Get detailed error information from auth-errors utility const errorDetails = getAuthErrorDetails(errorCode); // Set persistent error display setErrorTitle(errorDetails.title); setError(errorDetails.description); // Show error toast with conditional retry action const toastOptions: any = { id: loadingToast, description: errorDetails.description, duration: 6000, }; // Add retry action if the error is retryable if (shouldRetry(errorCode)) { toastOptions.action = { label: "Retry", onClick: () => handleSubmit(e), }; } toast.error(errorDetails.title, toastOptions); } finally { setIsLoading(false); } }; return (

Create an Account

{/* Enhanced Error Display */} {error && errorTitle && (
Error Icon

{errorTitle}

{error}

)}
setEmail(e.target.value)} className={`mt-1 block w-full rounded-md border px-3 py-2 shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2 dark:bg-gray-800 dark:text-white transition-colors ${ error ? "border-red-300 focus:border-red-500 focus:ring-red-500 dark:border-red-700" : "border-gray-300 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-700" }`} disabled={isLoading} />
setPassword(e.target.value)} className={`mt-1 block w-full rounded-md border px-3 py-2 shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2 dark:bg-gray-800 dark:text-white transition-colors ${ error ? "border-red-300 focus:border-red-500 focus:ring-red-500 dark:border-red-700" : "border-gray-300 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-700" }`} disabled={isLoading} />
setConfirmPassword(e.target.value)} className={`mt-1 block w-full rounded-md border px-3 py-2 shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2 dark:bg-gray-800 dark:text-white transition-colors ${ error ? "border-red-300 focus:border-red-500 focus:ring-red-500 dark:border-red-700" : "border-gray-300 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-700" }`} disabled={isLoading} />

Already have an account?{" "} Sign in

); }