import { useState } from 'react' import { Link } from 'react-router-dom' import { authClient } from '../lib/auth-client.ts' import { useAuthStore } from '../stores/auth.ts' export function Login() { const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) const setAuthenticated = useAuthStore((s) => s.setAuthenticated) async function handleSubmit(e: React.FormEvent) { e.preventDefault() setError('') if (!email || !password) { setError('Please fill in all fields.') return } setLoading(true) try { const { error: authError } = await authClient.signIn.email({ email, password, }) if (authError) { throw new Error(authError.message ?? 'Sign in failed') } // After successful signIn, force a full page reload so Better-Auth's // useSession() reinitializes with fresh cookie-backed session state. // Using React Router's navigate() races with Better-Auth's internal update. const sessionResult = await authClient.getSession() if (sessionResult.data) { window.location.href = '/' } else { setError('Sign in failed. Please try again.') } } catch { if (import.meta.env.VITE_MOCK_AUTH === 'true') { setAuthenticated(true) window.location.href = '/' } else { setError('Invalid email or password. Please try again.') } } finally { setLoading(false) } } return (

CartSnitch

Track prices. Save money.

{error && (
{error}
)}
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="current-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" />
Forgot password?

Don't have an account?{' '} Sign up

) }