import React from 'react'; import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Mail } from 'lucide-react'; import { useMutation } from '@tanstack/react-query'; import { YStack, XStack } from '@tamagui/stacks'; import { SizableText as Text } from '@tamagui/text'; import { Button } from '@tamagui/button'; import { ADMIN_LOGIN_PATH } from '../constants'; import { MobileCard } from './components/Primitives'; import { MobileField, MobileInput } from './components/FormControls'; import { ADMIN_GRADIENTS, useAdminTheme } from './theme'; type ResetResponse = { status: string; }; async function requestPasswordReset(email: string): Promise { const response = await fetch('/api/v1/tenant-auth/forgot-password', { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json', }, body: JSON.stringify({ email }), }); if (response.status === 422) { const data = await response.json(); const errors = data.errors ?? {}; const flattened = Object.values(errors).flat(); throw new Error(flattened.join(' ') || 'Validation failed'); } if (!response.ok) { throw new Error('Request failed.'); } return (await response.json()) as ResetResponse; } export default function ForgotPasswordPage() { const { t } = useTranslation('auth'); const navigate = useNavigate(); const { text, muted, border, surface, accentSoft, primary, dangerBg, dangerText } = useAdminTheme(); const [email, setEmail] = React.useState(''); const [status, setStatus] = React.useState(null); const [error, setError] = React.useState(null); const safeAreaStyle: React.CSSProperties = { paddingTop: 'calc(env(safe-area-inset-top, 0px) + 16px)', paddingBottom: 'calc(env(safe-area-inset-bottom, 0px) + 16px)', }; const mutation = useMutation({ mutationKey: ['tenantAdminForgotPassword'], mutationFn: requestPasswordReset, onSuccess: (data) => { setError(null); setStatus(data.status ?? t('login.forgot.success', 'If your account exists, we will send a reset link.')); }, onError: (err: unknown) => { const message = err instanceof Error ? err.message : String(err); setError(message); }, }); return ( {t('login.forgot.title', 'Forgot your password?')} {t('login.forgot.copy', 'Enter your email address and we will send you a reset link.')} setEmail(event.target.value)} placeholder={t('login.email_placeholder', 'name@example.com')} autoComplete="email" required /> {status ? ( {status} ) : null} {error ? ( {error} ) : null} ); }