import { useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { authClient } from '../lib/auth-client.ts' export function Register() { const [name, setName] = useState('') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) const navigate = useNavigate() async function handleSubmit(e: React.FormEvent) { e.preventDefault() setError('') if (!name || !email || !password) { setError('Please fill in all fields.') return } if (password.length < 8) { setError('Password must be at least 8 characters.') return } setLoading(true) try { const { error: authError } = await authClient.signUp.email({ name, email, password, }) if (authError) { throw new Error(authError.message ?? 'Registration failed') } // After successful signUp, force a session fetch to confirm the cookie is set // before navigating to the protected route const sessionResult = await authClient.getSession() if (sessionResult.data) { navigate('/') } else { // Session not established — show success message and link to login setError('Account created! Please sign in.') } } catch { if (import.meta.env.VITE_MOCK_AUTH === 'true') { navigate('/') } else { setError('Registration failed. Please try again.') } } finally { setLoading(false) } } return (
Start tracking your grocery prices.
{error && (Already have an account?{' '} Sign in