import { useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { useAuthStore } from '../stores/auth.ts' import { api } from '../lib/api.ts' import { mockUser } from '../lib/mock-data.ts' import type { User } from '../types/api.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() const setAuth = useAuthStore((s) => s.setAuth) 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 res = await api.post<{ user: User; token: string }>('/auth/register', { name, email, password }) setAuth(res.user, res.token) navigate('/') } catch (err) { if (import.meta.env.VITE_MOCK_AUTH === 'true') { // Fallback to mock auth for demo setAuth({ ...mockUser, name, email }, 'mock-jwt-token') navigate('/') } else { setError('Registration failed. Please try again.') } } finally { setLoading(false) } } return (

Create Account

Start tracking your grocery prices.

{error && (
{error}
)}
setName(e.target.value)} autoComplete="name" className="min-h-12 w-full rounded-xl border border-gray-200 px-4 text-base focus:border-brand-blue focus:outline-none focus:ring-1 focus:ring-brand-blue" /> setEmail(e.target.value)} autoComplete="email" className="min-h-12 w-full rounded-xl border border-gray-200 px-4 text-base focus:border-brand-blue focus:outline-none focus:ring-1 focus:ring-brand-blue" /> setPassword(e.target.value)} autoComplete="new-password" className="min-h-12 w-full rounded-xl border border-gray-200 px-4 text-base focus:border-brand-blue focus:outline-none focus:ring-1 focus:ring-brand-blue" />

Already have an account?{' '} Sign in

) }