90 lines
3.2 KiB
TypeScript
90 lines
3.2 KiB
TypeScript
/**
|
|
* Request a password reset link by email.
|
|
*/
|
|
import { Alert, Box, Button, Card, CardContent, Link, Stack, TextField, Typography } from '@mui/material';
|
|
import { useState } from 'react';
|
|
import { Link as RouterLink } from 'react-router-dom';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { api } from '../../shared/services/api';
|
|
import { LanguageSwitcher } from '../../shared/i18n/LanguageSwitcher';
|
|
|
|
export function ForgotPasswordPage() {
|
|
const { t } = useTranslation();
|
|
const [email, setEmail] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [success, setSuccess] = useState<string | null>(null);
|
|
const [devUrl, setDevUrl] = useState<string | null>(null);
|
|
|
|
const submit = async () => {
|
|
setError(null);
|
|
setSuccess(null);
|
|
setDevUrl(null);
|
|
if (!email.trim()) {
|
|
setError(t('auth.emailRequiredShort'));
|
|
return;
|
|
}
|
|
setLoading(true);
|
|
try {
|
|
const res = await api.post<{ message: string; devResetUrl?: string }>(
|
|
'/api/v1/auth/forgot-password',
|
|
{ email: email.trim() }
|
|
);
|
|
setSuccess(res.data.message);
|
|
if (res.data.devResetUrl) setDevUrl(res.data.devResetUrl);
|
|
} catch (e: unknown) {
|
|
const err = e as { response?: { data?: { error?: string } }; message?: string };
|
|
setError(err?.response?.data?.error ?? err?.message ?? t('auth.requestFailed'));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Box sx={{ minHeight: '100vh', display: 'grid', placeItems: 'center', px: 2, position: 'relative' }}>
|
|
<Box sx={{ position: 'absolute', top: 16, right: 16 }}>
|
|
<LanguageSwitcher />
|
|
</Box>
|
|
<Card sx={{ width: '100%', maxWidth: 520 }}>
|
|
<CardContent>
|
|
<Typography variant="h4" sx={{ fontWeight: 900 }}>
|
|
{t('auth.resetPassword')}
|
|
</Typography>
|
|
<Typography sx={{ opacity: 0.75, mt: 0.5 }}>{t('auth.resetPasswordSubtitle')}</Typography>
|
|
|
|
<Stack spacing={2} sx={{ mt: 3 }}>
|
|
{error && <Alert severity="error">{error}</Alert>}
|
|
{success && <Alert severity="success">{success}</Alert>}
|
|
{devUrl && (
|
|
<Alert severity="info">
|
|
<Typography variant="body2" sx={{ mb: 1 }}>
|
|
{t('auth.consoleResetHint')}
|
|
</Typography>
|
|
<Link href={devUrl} target="_blank" rel="noreferrer" sx={{ wordBreak: 'break-all' }}>
|
|
{devUrl}
|
|
</Link>
|
|
</Alert>
|
|
)}
|
|
<TextField
|
|
label={t('common.email')}
|
|
type="email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
fullWidth
|
|
autoComplete="email"
|
|
/>
|
|
<Button variant="contained" onClick={submit} disabled={loading}>
|
|
{loading ? t('common.sending') : t('auth.sendResetLink')}
|
|
</Button>
|
|
<Typography variant="body2" sx={{ textAlign: 'center', opacity: 0.8 }}>
|
|
<Link component={RouterLink} to="/login">
|
|
{t('auth.backToSignIn')}
|
|
</Link>
|
|
</Typography>
|
|
</Stack>
|
|
</CardContent>
|
|
</Card>
|
|
</Box>
|
|
);
|
|
}
|