'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 (
Health management made simple
Don't have an account?{' '} Create one