'use client' import { useState } from 'react' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Checkbox } from '@/components/ui/checkbox' import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from '@/components/ui/card' import { useAuth } from '@/contexts/AuthContext' import { GoogleSignInButton } from './GoogleSignInButton' import { GitHubSignInButton } from './GitHubSignInButton' import { User, Mail, Phone, Lock, Eye, EyeOff, Check, X } from 'lucide-react' import Link from 'next/link' const RegisterSchema = z .object({ name: z.string().min(1, 'Name is required'), email: z.string().email('Invalid email format'), phone: z.string().optional(), password: z .string() .min(6, 'Password must be at least 6 characters') .regex(/[A-Z]/, 'Password must contain at least one uppercase letter') .regex(/[0-9!@#$%^&*]/, 'Password must contain at least one number or special character'), confirmPassword: z.string().min(1, 'Please confirm your password'), agreeTerms: z.boolean().refine((val) => val === true, { message: 'You must agree to the Terms of Service and Privacy Policy', }), }) .refine((data) => data.password === data.confirmPassword, { message: "Passwords don't match", path: ['confirmPassword'], }) type RegisterFormData = z.infer interface RegisterFormProps { onSwitchToLogin?: () => void } export function RegisterForm({ onSwitchToLogin }: RegisterFormProps) { const [error, setError] = useState(null) const [showPassword, setShowPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [password, setPassword] = useState('') const { register: registerUser, loading } = useAuth() const { register, handleSubmit, formState: { errors }, watch, setValue, getValues, trigger, } = useForm({ resolver: zodResolver(RegisterSchema), }) const watchPassword = watch('password', '') // Password strength validation const passwordRequirements = { minLength: watchPassword.length >= 6, hasUppercase: /[A-Z]/.test(watchPassword), hasNumberOrSpecial: /[0-9!@#$%^&*]/.test(watchPassword), } const onSubmit = async (data: RegisterFormData) => { try { setError(null) await registerUser(data.email, data.name, data.password) } catch (err) { setError(err instanceof Error ? err.message : 'Registration failed') } } return ( Create Account Fill in your details to create a new account
{error && (
{error}
)}
{errors.name &&

{errors.name.message}

}
{errors.email &&

{errors.email.message}

}
{errors.phone &&

{errors.phone.message}

}
{/* Password strength indicators */} {watchPassword && (
{passwordRequirements.minLength ? ( ) : ( )} Minimum 6 characters
{passwordRequirements.hasUppercase ? ( ) : ( )} One uppercase letter
{passwordRequirements.hasNumberOrSpecial ? ( ) : ( )} One number or special character
)} {errors.password &&

{errors.password.message}

}
{errors.confirmPassword && (

{errors.confirmPassword.message}

)}
{ setValue('agreeTerms', checked as boolean) trigger('agreeTerms') }} />
{errors.agreeTerms &&

{errors.agreeTerms.message}

}
Or continue with
Already have an account?{' '} {onSwitchToLogin ? ( ) : ( Sign in )}
) }