import { useState } from 'react'; import { Container, Paper, Title, Text, TextInput, PasswordInput, Button, Anchor, Stack, Alert, } from '@mantine/core'; import { useForm } from '@mantine/form'; import { IconAlertCircle } from '@tabler/icons-react'; import { useNavigate, Link } from 'react-router-dom'; import api from '../../services/api'; import { useAuthStore } from '../../stores/authStore'; export function LoginPage() { const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const navigate = useNavigate(); const setAuth = useAuthStore((s) => s.setAuth); const form = useForm({ initialValues: { email: '', password: '' }, validate: { email: (v) => (/^\S+@\S+$/.test(v) ? null : 'Invalid email'), password: (v) => (v.length >= 1 ? null : 'Password required'), }, }); const handleSubmit = async (values: typeof form.values) => { setLoading(true); setError(''); try { const { data } = await api.post('/auth/login', values); setAuth(data.accessToken, data.user, data.organizations); // Always go through org selection to ensure correct JWT with orgSchema if (data.organizations.length >= 1) { navigate('/select-org'); } else { navigate('/'); } } catch (err: any) { setError(err.response?.data?.message || 'Login failed'); } finally { setLoading(false); } }; return ( HOA Financial Platform Don't have an account?{' '} Register
{error && ( } color="red" variant="light"> {error} )}
); }