Support new backend contracts: single-flight refresh, ad impression tokens, error handling

- apiFetch: proactive refresh, cross-tab single-flight via Web Locks, no logout on transient errors
- AuthContext: save tokens returned on own login change, revoke refresh on logout, sync tabs
- Ads: X-Session-Id, impressionToken in impression/click, IAB viewability, safe http(s) targetUrl
- STOMP: refresh before CONNECT, stop reconnect loop on dead session, /user/queue/errors behind flag
- Auth form: password 8 chars / 72 bytes; 429/413 messages via describeApiError
- My posts with auth + moderation badges; post form step validation and field limits
- Map: escape user text in Yandex hint/template

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-20 02:03:42 +03:00
parent 67de963d6d
commit db37befb8d
34 changed files with 1005 additions and 260 deletions

View File

@@ -4,6 +4,8 @@ import { useTranslation } from 'react-i18next';
import CloseIcon from "./icons/CloseIcon";
import { useAuthContext } from "../context/AuthContext";
import { login as apiLogin, register as apiRegister } from "../api/apiClient";
import { describeApiError } from "../api/apiErrors";
import { validatePassword } from "../utils/passwordPolicy";
/**
* AuthModal — модальное окно входа и регистрации прямо на странице ленты.
@@ -63,13 +65,13 @@ function LoginFormInline({ saveAuth, onClose, onSwitch }) {
setLoading(true); setError("");
try {
const { token, refreshToken, user } = await apiLogin(
login, password, t('auth.invalidCredentials')
);
const { token, refreshToken, user } = await apiLogin(login, password);
saveAuth(user, token, refreshToken);
onClose();
} catch (err) {
setError(err.message);
// 400 — неверные данные (текст с сервера), 429 — лимит попыток или
// блокировка аккаунта на 15 минут после 10 неудачных входов
setError(describeApiError(err, t('auth.invalidCredentials')));
} finally {
setLoading(false);
}
@@ -85,7 +87,8 @@ function LoginFormInline({ saveAuth, onClose, onSwitch }) {
placeholder={t('auth.yourLogin')}
value={login}
onChange={e => { setLogin(e.target.value); setError(""); }}
autoComplete="off"
autoComplete="username"
autoCapitalize="none"
/>
</div>
@@ -95,6 +98,7 @@ function LoginFormInline({ saveAuth, onClose, onSwitch }) {
className="f-m-field"
type="password"
placeholder={t('auth.yourPassword')}
autoComplete="current-password"
value={password}
onChange={e => { setPassword(e.target.value); setError(""); }}
/>
@@ -136,7 +140,8 @@ function RegisterFormInline({ saveAuth, onClose, onSwitch }) {
if (!form.name.trim()) e.name = t('auth.validateNameRequired');
if (!form.login.trim()) e.login = t('auth.validateLoginRequired');
else if (form.login.length < 3) e.login = t('auth.loginMinChars');
if (form.password.length < 6) e.password = t('auth.validatePasswordMin');
const passwordError = validatePassword(form.password);
if (passwordError) e.password = passwordError;
if (form.password !== form.password2) e.password2 = t('auth.validatePasswordMatch');
setErrors(e);
return !Object.keys(e).length;
@@ -147,21 +152,24 @@ function RegisterFormInline({ saveAuth, onClose, onSwitch }) {
if (!validate()) return;
setLoading(true); setError("");
let registered = false;
try {
await apiRegister(form.name, form.login, form.password, t('auth.registrationError'));
let session;
try {
session = await apiLogin(form.login, form.password, t('auth.autoLoginFailed'));
} catch {
throw new Error(t('auth.accountCreatedLoginManually'));
// Регистрация сразу возвращает {token, refreshToken, user}. Отдельный
// вход нужен только если бэкенд их не прислал — лишний запрос к
// /auth/login тратил бы лимит попыток входа.
let session = await apiRegister(form.name, form.login, form.password);
registered = true;
if (!session?.token) {
session = await apiLogin(form.login, form.password);
}
const { token, refreshToken, user } = session;
saveAuth(user, token, refreshToken);
onClose();
} catch (err) {
setError(err.message);
setError(registered
? t('auth.accountCreatedLoginManually')
: describeApiError(err, t('auth.registrationError')));
} finally {
setLoading(false);
}
@@ -187,6 +195,8 @@ function RegisterFormInline({ saveAuth, onClose, onSwitch }) {
className={`f-m-field${errors.login ? " err" : ""}`}
type="text"
placeholder={t('auth.loginPlaceholder')}
autoComplete="username"
autoCapitalize="none"
value={form.login}
onChange={e => setField("login", e.target.value)}
/>
@@ -200,6 +210,7 @@ function RegisterFormInline({ saveAuth, onClose, onSwitch }) {
className={`f-m-field${errors.password ? " err" : ""}`}
type="password"
placeholder={t('auth.passwordMinShort')}
autoComplete="new-password"
value={form.password}
onChange={e => setField("password", e.target.value)}
/>
@@ -211,6 +222,7 @@ function RegisterFormInline({ saveAuth, onClose, onSwitch }) {
className={`f-m-field${errors.password2 ? " err" : ""}`}
type="password"
placeholder={t('auth.repeatPlaceholder')}
autoComplete="new-password"
value={form.password2}
onChange={e => setField("password2", e.target.value)}
/>