import React, { useState } from 'react'; import { useAuth } from '../../contexts/AuthContext'; import { Button } from '../ui/button'; import { Input } from '../ui/input'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '../ui/card'; import { Label } from '../ui/label'; interface RegisterFormProps { onSuccess?: () => void; onSwitchToLogin?: () => void; } export function RegisterForm({ onSuccess, onSwitchToLogin }: RegisterFormProps) { const { register, isLoading } = useAuth(); const [username, setUsername] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [error, setError] = useState(''); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); if (password !== confirmPassword) { setError('Passwords do not match'); return; } if (password.length < 6) { setError('Password must be at least 6 characters'); return; } try { await register(username, email, password); onSuccess?.(); } catch (err) { setError(err instanceof Error ? err.message : 'Registration failed'); } }; return ( Register Create a new account to start commenting and reacting
{error && (
{error}
)}
setUsername(e.target.value)} placeholder="Choose a username" required disabled={isLoading} />
setEmail(e.target.value)} placeholder="Enter your email" required disabled={isLoading} />
setPassword(e.target.value)} placeholder="Choose a password (min 6 characters)" required disabled={isLoading} />
setConfirmPassword(e.target.value)} placeholder="Confirm your password" required disabled={isLoading} />
{onSwitchToLogin && (
Already have an account?{' '}
)}
); }