'use client' import React, { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' import { Loader2 } from 'lucide-react' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Alert, AlertDescription } from '@/components/ui/alert' export default function CaregiverLoginPage() { const router = useRouter() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState(null) const [loading, setLoading] = useState(false) const [checking, setChecking] = useState(true) useEffect(() => { const checkAuth = async () => { try { const res = await fetch('/api/users/me', { credentials: 'include' }) if (res.ok) { const data = await res.json() if (data.user) { router.push('/caregiver/dashboard') return } } } catch { // Not logged in } setChecking(false) } checkAuth() }, [router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError(null) setLoading(true) try { const res = await fetch('/api/users/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), credentials: 'include', }) const data = await res.json() if (!res.ok) { throw new Error(data.errors?.[0]?.message || 'Login failed') } const user = data.user const hasCaregiverRole = user?.roles?.includes('super-admin') || user?.tenants?.some( (t: { roles?: string[] }) => t.roles?.includes('caregiver') || t.roles?.includes('admin'), ) if (!hasCaregiverRole) { await fetch('/api/users/logout', { method: 'POST', credentials: 'include', }) throw new Error('You do not have caregiver access') } router.push('/caregiver/dashboard') } catch (err) { setError(err instanceof Error ? err.message : 'Login failed') } finally { setLoading(false) } } if (checking) { return (
) } return (

Meal Planner

Caregiver Portal

Login Enter your credentials to access the caregiver portal {error && ( {error} )}
setEmail(e.target.value)} placeholder="Enter your email" required autoComplete="email" />
setPassword(e.target.value)} placeholder="Enter your password" required autoComplete="current-password" />
) }