'use client' import { Suspense, useState } from 'react' import { useRouter, useSearchParams } from 'next/navigation' import Link from 'next/link' import { Heart } from 'lucide-react' import { Button, Input, Card, showToast } from '@/components/ui' function LoginForm() { const router = useRouter() const searchParams = useSearchParams() const redirectTo = searchParams.get('redirect') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') setLoading(true) try { const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ email, password }), }) const data = await response.json() if (!response.ok) { setError(data.error || 'Login failed') return } // Check if user needs to change password if (data.forcePasswordReset) { showToast('Please change your password to continue', 'info') router.push('/change-password') router.refresh() return } const sessionResponse = await fetch('/api/auth/me', { credentials: 'include', cache: 'no-store', }) if (!sessionResponse.ok) { throw new Error('Your session was created but is not available yet. Please try again.') } showToast('Welcome back!', 'success') // If there's a redirect param (e.g., from invite link), go there router.push(redirectTo || '/today') router.refresh() } catch { setError('Something went wrong. Please try again.') } finally { setLoading(false) } } return (
{/* Logo */}

Next Step

Health management made simple

setEmail(e.target.value)} placeholder="you@example.com" required autoComplete="email" /> setPassword(e.target.value)} placeholder="••••••••" required autoComplete="current-password" /> {error && (

{error}

)}

Don't have an account?{' '} Create one

) } export default function LoginPage() { return (
}>
) }