"use client" import type React from "react" import { useState } from "react" import { useRouter } from "next/navigation" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Alert, AlertDescription } from "@/components/ui/alert" import { Checkbox } from "@/components/ui/checkbox" import { useAuth } from "@/contexts/supabase-auth-context" import { Cpu, Eye, EyeOff, Loader2, Mail, CheckCircle } from "lucide-react" export default function RegisterPage() { const [step, setStep] = useState<'register' | 'verify'>('register') const [email, setEmail] = useState("") const [verificationCode, setVerificationCode] = useState("") const [username, setUsername] = useState("") const [password, setPassword] = useState("") const [confirmPassword, setConfirmPassword] = useState("") const [showPassword, setShowPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [acceptTerms, setAcceptTerms] = useState(false) const [error, setError] = useState("") const [successMessage, setSuccessMessage] = useState("") const [isSendingCode, setIsSendingCode] = useState(false) const [isVerifying, setIsVerifying] = useState(false) const [verificationToken, setVerificationToken] = useState(null) const router = useRouter() const { register, sendVerificationCode, verifyCode, isLoading } = useAuth() // Step 1: Submit user details and send verification code const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError("") setSuccessMessage("") if (!username || !email || !password || !confirmPassword) { setError("All fields are required") return } if (password !== confirmPassword) { setError("Passwords do not match") return } if (password.length < 6) { setError("Password must be at least 6 characters long") return } if (!acceptTerms) { setError("Please accept the terms and conditions") return } // Validate email format const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ if (!emailRegex.test(email)) { setError("Please enter a valid email address") return } // Send verification code setIsSendingCode(true) const result = await sendVerificationCode(email) setIsSendingCode(false) if (result.success) { setStep('verify') setSuccessMessage("Verification code sent to your email. Please check your inbox.") } else { setError(result.error || "Failed to send verification code") } } // Step 2: Verify code and complete registration const handleVerifyCode = async (e: React.FormEvent) => { e.preventDefault() setError("") setSuccessMessage("") if (!verificationCode || verificationCode.length !== 6) { setError("Please enter the 6-digit verification code") return } setIsVerifying(true) const verifyResult = await verifyCode(email, verificationCode) if (verifyResult.success && verifyResult.token) { setVerificationToken(verifyResult.token) // Now complete registration with the token const registerResult = await register(username, email, password, verifyResult.token) setIsVerifying(false) if (registerResult.success) { setSuccessMessage("Account created successfully! Redirecting to login...") setTimeout(() => { router.push("/login") }, 2000) } else { setError(registerResult.error || "Registration failed") } } else { setIsVerifying(false) setError(verifyResult.error || "Invalid verification code") } } return (
{/* BuildMate Logo - Centered */}

BuildMate

{/* Header */}

Create Account

{step === 'register' && 'Enter your details to create your account'} {step === 'verify' && 'Enter the verification code sent to your email'}

{step === 'register' && 'Step 1: Enter Your Details'} {step === 'verify' && 'Step 2: Verify Email'} {step === 'register' && 'We\'ll send a verification code to your email after you submit'} {step === 'verify' && `Code sent to ${email}. Enter the 6-digit code to complete registration.`} {error && ( {error} )} {successMessage && ( {successMessage} )} {/* Step 1: Registration Form */} {step === 'register' && (
setUsername(e.target.value)} required disabled={isSendingCode} />
setEmail(e.target.value)} required disabled={isSendingCode} />

We'll send a 6-digit verification code to your verified email address (Gmail, Yahoo, Outlook, etc.)

setPassword(e.target.value)} required disabled={isSendingCode} />
setConfirmPassword(e.target.value)} required disabled={isSendingCode} />
setAcceptTerms(checked as boolean)} disabled={isSendingCode} />
)} {/* Step 2: Verification Code */} {step === 'verify' && (
setVerificationCode(e.target.value.replace(/\D/g, '').slice(0, 6))} required maxLength={6} disabled={isVerifying} className="text-center text-2xl font-mono tracking-widest" />

Check your email inbox for the verification code

)}

Already have an account?{" "} Sign in

← Back to Home
) }