import { useState, useEffect } from 'react' import { Link, useNavigate } from 'react-router' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { toast } from 'sonner' import { useAuth } from '@/hooks/use-auth' import { authService } from '@/services/auth' export function RegisterPage() { const [formData, setFormData] = useState({ name: '', email: '', password: '', confirmPassword: '', }) const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [oauthProviders, setOauthProviders] = useState>({}) const { register, user } = useAuth() const navigate = useNavigate() // Redirect if already logged in useEffect(() => { if (user) { navigate('/') } }, [user, navigate]) // Load OAuth providers useEffect(() => { const loadProviders = async () => { try { const providers = await authService.getOAuthProviders() if (providers) { setOauthProviders(providers) } } catch (error) { console.error('Failed to load OAuth providers:', error) } } loadProviders() }, []) const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target setFormData(prev => ({ ...prev, [name]: value })) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError('') // Validation if (formData.password !== formData.confirmPassword) { setError('Passwords do not match') setLoading(false) return } if (formData.password.length < 6) { setError('Password must be at least 6 characters long') setLoading(false) return } try { await register(formData.email, formData.password, formData.name) toast.success('Account created successfully! Welcome!') navigate('/') } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Registration failed' setError(errorMessage) toast.error(errorMessage) } finally { setLoading(false) } } const handleOAuthLogin = (provider: string) => { window.location.href = authService.getOAuthLoginUrl(provider) } return (
Create account Enter your information to create your account {error && (
{error}
)}
{Object.keys(oauthProviders).length > 0 && ( <>
Or continue with
{Object.entries(oauthProviders).map(([key, provider]) => ( ))}
)}
Already have an account?{' '} Sign in
) }