'use client'; import { useEffect, useState, useTransition } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { createSupabaseBrowserClient } from '@/lib/supabase/browser'; const RESEND_COOLDOWN = 45; export default function SignupPage() { const router = useRouter(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(null); const [submitted, setSubmitted] = useState(false); const [resendInfo, setResendInfo] = useState(null); const [cooldown, setCooldown] = useState(0); const [isPending, startTransition] = useTransition(); const [isResending, startResend] = useTransition(); useEffect(() => { if (cooldown <= 0) return; const t = setTimeout(() => setCooldown((c) => c - 1), 1000); return () => clearTimeout(t); }, [cooldown]); function onSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); setResendInfo(null); const supabase = createSupabaseBrowserClient(); startTransition(async () => { const { data, error } = await supabase.auth.signUp({ email, password, options: { emailRedirectTo: `${process.env.NEXT_PUBLIC_APP_URL ?? ''}/auth/confirm`, }, }); if (error) { setError(error.message); return; } // GoTrue returns a decoy user with an empty `identities` array when the // email is already registered (to avoid leaking existence). Treat it as // "already registered" and point them at login. if (data.user && data.user.identities?.length === 0) { setError( 'An account with this email already exists. Try signing in instead.', ); return; } if (data.session) { router.push('/dashboard'); router.refresh(); return; } setSubmitted(true); setCooldown(RESEND_COOLDOWN); }); } function onResend() { setError(null); setResendInfo(null); const supabase = createSupabaseBrowserClient(); startResend(async () => { const { error } = await supabase.auth.resend({ type: 'signup', email }); if (error) { setError(error.message); return; } setResendInfo('Confirmation email sent. Check your inbox.'); setCooldown(RESEND_COOLDOWN); }); } if (submitted) { return (

Check your email

We sent a confirmation link to {email}. Click the link in that email to activate your account, then sign in.

{error &&

{error}

} {resendInfo &&

{resendInfo}

}
Back to login
); } return (

Sign up

setEmail(e.target.value)} /> setPassword(e.target.value)} /> {error &&

{error}

}
Already have one?
); }