NTP_V2/web/src/shell/LoginPage.tsx

96 lines
4.8 KiB
TypeScript

import { useState, type FormEvent } from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { Alert, Box, Button, Card, CardContent, IconButton, InputAdornment, Stack, TextField, Typography, useTheme } from '@mui/material';
import VisibilityIcon from '@mui/icons-material/Visibility';
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff';
import { ApiError } from '@/api/client';
import { Logo } from '@/components/Logo';
import { useUi } from '@/i18n/useUi';
import { useAuth } from '@/providers/AuthProvider';
import { LoginHero } from './LoginHero';
import { ThemeToggle } from './Toggles';
/** Pre-login page: uses ONLY the built-in ui table (no server captions exist before sign-in). */
export function LoginPage() {
const { t } = useUi();
const theme = useTheme();
const { status, login } = useAuth();
const location = useLocation();
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [show, setShow] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
if (status === 'authed') {
const from = (location.state as { from?: string } | null)?.from;
return <Navigate to={from && from !== '/login' ? from : '/'} replace />;
}
async function submit(e: FormEvent) {
e.preventDefault();
if (!username.trim() || !password) { setError(t('login.required')); return; }
setBusy(true);
setError('');
try {
await login(username.trim(), password);
} catch (err) {
// backend message verbatim; our own text only when there is none
setError(err instanceof ApiError && err.status !== 0 && err.message ? err.message : err instanceof ApiError ? err.message : t('login.failed'));
} finally {
setBusy(false);
}
}
return (
// the brand panel (first child) sits on the physical right in both directions (the RTL style plugin would flip a "direction" rule)
<Box sx={{ minHeight: '100vh', display: 'flex', flexDirection: theme.direction === 'rtl' ? 'row' : 'row-reverse', bgcolor: 'background.default' }}>
<LoginHero />
<Box sx={{ flex: 1, display: 'flex', flexDirection: 'column', minWidth: 0 }}>
<Stack direction="row" spacing={0.5} sx={{ alignItems: 'center', justifyContent: 'flex-end', p: 1.5, color: 'text.primary' }}>
<ThemeToggle />
</Stack>
<Box sx={{ flex: 1, display: 'grid', placeItems: 'center', p: 2 }}>
<Card sx={{ width: '100%', maxWidth: { xs: 420, md: 520 }, borderRadius: 3, boxShadow: { xs: 'none', md: '0 10px 40px rgba(11,58,92,0.12)' }, borderColor: { md: 'transparent' } }} variant="outlined">
<CardContent sx={{ p: { xs: 3, sm: 4, md: 6 }, pt: { md: 5 } }}>
<Stack spacing={{ xs: 3, md: 3.5 }} component="form" onSubmit={submit} noValidate>
<Box sx={{ textAlign: 'center' }}>
{/* on wide screens the logo is in the brand panel */}
<Box sx={{ display: { xs: 'block', md: 'none' } }}><Logo height={44} /></Box>
<Typography variant="h4" sx={{ mt: { xs: 2, md: 0 }, fontWeight: 400 }}>{t('login.heroTitle')}</Typography>
<Typography color="text.secondary" sx={{ mt: 2, fontSize: { md: 16 } }}>{t('login.subtitle')}</Typography>
</Box>
{error && <Alert severity="error" role="alert">{error}</Alert>}
<TextField
label={t('login.username')} value={username} onChange={(e) => setUsername(e.target.value)}
autoComplete="username" autoFocus required slotProps={{ htmlInput: { dir: 'ltr', style: { textAlign: 'start' } } }}
/>
<TextField
label={t('login.password')} value={password} onChange={(e) => setPassword(e.target.value)}
type={show ? 'text' : 'password'} autoComplete="current-password" required
slotProps={{
htmlInput: { dir: 'ltr' },
input: {
endAdornment: (
<InputAdornment position="end">
<IconButton edge="end" onClick={() => setShow((s) => !s)} aria-label={show ? t('login.hidePassword') : t('login.showPassword')}>
{show ? <VisibilityOffIcon /> : <VisibilityIcon />}
</IconButton>
</InputAdornment>
),
},
}}
/>
<Button type="submit" variant="contained" size="large" disabled={busy}>
{busy ? t('login.submitting') : t('login.submit')}
</Button>
</Stack>
</CardContent>
</Card>
</Box>
<Typography variant="caption" color="text.secondary" align="center" sx={{ p: 2 }}>{t('login.copyright')} - {new Date().getFullYear()}</Typography>
</Box>
</Box>
);
}