diff --git a/app/src/api/client.ts b/app/src/api/client.ts index 32083c2..9a6f5f0 100644 --- a/app/src/api/client.ts +++ b/app/src/api/client.ts @@ -8,6 +8,11 @@ export class ApiError extends Error { ) { super(body?.error?.message || `HTTP ${status}`); } + + /** Kod błędu z ciała odpowiedzi (`{ error: { code } }`) albo null. */ + get code(): string | null { + return this.body?.error?.code ?? null; + } } let accessToken: string | null = localStorage.getItem('fitos_access'); diff --git a/app/src/lib/errors.ts b/app/src/lib/errors.ts new file mode 100644 index 0000000..e30f96f --- /dev/null +++ b/app/src/lib/errors.ts @@ -0,0 +1,38 @@ +import { ApiError } from '../api/client'; +import { resolveLocaleTag } from '../i18n/language'; +import { i18n } from '../i18n'; + +type TFn = (key: string, options?: Record) => string; + +/** + * Etykieta błędu API dla użytkownika: kod → `errors:`, z obsługą + * `details.until` (`errors:_until`) i `details.reason` (sufiks). + * Bez mapowania — surowy message; bez message — fallback. + */ +export function apiErrorMessage(err: unknown, t: TFn, fallback?: string): string { + if (err instanceof ApiError) { + const code = err.body?.error?.code; + if (code) { + const details = (err.body?.error?.details ?? {}) as { + until?: string; + reason?: string | null; + }; + let base = ''; + if (details.until) { + const date = new Intl.DateTimeFormat(resolveLocaleTag(i18n.language), { + dateStyle: 'long', + timeStyle: 'short', + }).format(new Date(details.until)); + base = t(`errors:${code}_until`, { date, defaultValue: '' }); + } + if (!base) base = t(`errors:${code}`, { defaultValue: '' }); + if (base) { + if (details.reason) base += t('errors:reasonSuffix', { reason: details.reason }); + return base; + } + } + return err.message || fallback || ''; + } + if (err instanceof Error) return err.message || fallback || ''; + return String(err); +} diff --git a/app/src/pages/Login.tsx b/app/src/pages/Login.tsx index 8a11ed8..01bbf8f 100644 --- a/app/src/pages/Login.tsx +++ b/app/src/pages/Login.tsx @@ -1,41 +1,15 @@ import { useState } from 'react'; +import { useTranslation } from 'react-i18next'; import { Link, useNavigate } from 'react-router-dom'; import { useAuth } from '../store/auth'; -import { ApiError } from '../api/client'; import AuthShell from '../components/AuthShell'; import { Button, ErrorText, Field, Input } from '../components/ui'; +import { apiErrorMessage } from '../lib/errors'; const REMEMBER_KEY = 'fitos-remembered-email'; -interface RestrictionDetails { - until?: string; - reason?: string | null; -} - -/** Kara na koncie (403 z API) ma własny komunikat z terminem/powodem. */ -function loginErrorMessage(err: unknown): string { - if (err instanceof ApiError) { - const code = err.body?.error?.code; - const details = err.body?.error?.details as RestrictionDetails | undefined; - if (code === 'ACCOUNT_SUSPENDED') { - const base = details?.until - ? `Konto zawieszone do ${new Intl.DateTimeFormat('pl-PL', { - dateStyle: 'long', - timeStyle: 'short', - }).format(new Date(details.until))}.` - : 'Konto zawieszone.'; - return details?.reason ? `${base} Powód: ${details.reason}` : base; - } - if (code === 'ACCOUNT_BANNED') { - return details?.reason - ? `Konto zablokowane przez administratora. Powód: ${details.reason}` - : 'Konto zablokowane przez administratora.'; - } - } - return err instanceof Error ? err.message : 'Błąd logowania'; -} - export default function Login() { + const { t } = useTranslation(); const { login } = useAuth(); const navigate = useNavigate(); const [email, setEmail] = useState(() => localStorage.getItem(REMEMBER_KEY) ?? ''); @@ -54,7 +28,7 @@ export default function Login() { else localStorage.removeItem(REMEMBER_KEY); navigate('/'); } catch (err) { - setError(loginErrorMessage(err)); + setError(apiErrorMessage(err, t, t('errors:loginFailed'))); } finally { setLoading(false); } @@ -63,7 +37,7 @@ export default function Login() { return (
- + - + setRemember(e.target.checked)} className="[--checkbox-size:1.25rem]" /> - Zapamiętaj mnie + {t('auth:rememberMe')} {error && {error}}

- Nie masz konta? Zarejestruj się + {t('auth:noAccount')} {t('auth:registerLink')}

); diff --git a/app/src/pages/Register.tsx b/app/src/pages/Register.tsx index 3545901..602aaf6 100644 --- a/app/src/pages/Register.tsx +++ b/app/src/pages/Register.tsx @@ -1,10 +1,13 @@ import { useState } from 'react'; +import { useTranslation } from 'react-i18next'; import { Link, useNavigate } from 'react-router-dom'; import { useAuth } from '../store/auth'; import AuthShell from '../components/AuthShell'; import { Button, ErrorText, Field, Input } from '../components/ui'; +import { apiErrorMessage } from '../lib/errors'; export default function Register() { + const { t } = useTranslation(); const { register } = useAuth(); const navigate = useNavigate(); const [form, setForm] = useState({ email: '', password: '', firstName: '', lastName: '' }); @@ -26,7 +29,7 @@ export default function Register() { }); navigate('/'); } catch (err) { - setError(err instanceof Error ? err.message : 'Błąd rejestracji'); + setError(apiErrorMessage(err, t, t('errors:registerFailed'))); } finally { setLoading(false); } @@ -35,7 +38,7 @@ export default function Register() { return (
- + - +
- + set('firstName', e.target.value)} autoComplete="given-name" /> - + set('lastName', e.target.value)} @@ -72,11 +75,11 @@ export default function Register() {
{error && {error}}

- Masz konto? Zaloguj się + {t('auth:haveAccount')} {t('auth:loginLink')}

); diff --git a/app/test/Login.test.tsx b/app/test/Login.test.tsx index 61f2570..45f39e4 100644 --- a/app/test/Login.test.tsx +++ b/app/test/Login.test.tsx @@ -128,7 +128,9 @@ describe('Login page', () => { await user.type(email, 'jan@test.local'); await user.type(password, 'wrong'); await user.click(screen.getByRole('button', { name: 'Zaloguj' })); - expect(await screen.findByText('Invalid credentials')).toBeInTheDocument(); + expect( + await screen.findByText('Sesja wygasła — zaloguj się ponownie.'), + ).toBeInTheDocument(); expect(useAuth.getState().user).toBeNull(); }); }); diff --git a/app/test/Register.test.tsx b/app/test/Register.test.tsx index 875f523..9d10cf6 100644 --- a/app/test/Register.test.tsx +++ b/app/test/Register.test.tsx @@ -93,7 +93,7 @@ describe('Register page', () => { await user.type(email, 'jan@test.local'); await user.type(password, 'Password123!'); await user.click(screen.getByRole('button', { name: 'Zarejestruj' })); - expect(await screen.findByText('Email already in use')).toBeInTheDocument(); + expect(await screen.findByText('Zmiana koliduje z istniejącymi danymi.')).toBeInTheDocument(); expect(useAuth.getState().user).toBeNull(); }); });