From e3f37744b0755080352c32afcf65da5f5b13a86e Mon Sep 17 00:00:00 2001 From: HotelSync Date: Tue, 17 Mar 2026 19:05:55 +0300 Subject: [PATCH] Improve login UX for unconfirmed email + resend confirmation - LoginForm: detect 403 separately, show amber warning with email address - Add 'Resend confirmation email' button (calls POST /api/auth/resend-confirmation) - Backend: POST /api/auth/resend-confirmation endpoint (generates new token, resends email) - api.ts: add resendConfirmation method Co-Authored-By: Claude Sonnet 4.6 --- backend/src/routes/auth.ts | 38 +++++++++++++++++++++++ src/lib/api.ts | 3 ++ src/pages/LoginPage.tsx | 63 +++++++++++++++++++++++++++++++------- 3 files changed, 93 insertions(+), 11 deletions(-) diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index fc6dfbe..50a950b 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -225,6 +225,44 @@ const auth: FastifyPluginAsync = async (fastify) => { }, ) + // ── POST /api/auth/resend-confirmation ──────────────────────────────────── + fastify.post<{ Body: { email: string } }>( + '/api/auth/resend-confirmation', + { + schema: { + body: { + type: 'object', + required: ['email'], + properties: { email: { type: 'string' } }, + }, + }, + }, + async (request, reply) => { + const { email } = request.body + const { rows } = await db.query( + `SELECT id, name FROM users WHERE email = $1 AND email_confirmed = false`, + [email.toLowerCase().trim()], + ) + // Always return 200 to prevent enumeration + if (rows.length === 0) return reply.code(200).send({ ok: true }) + + const user = rows[0] + const confirmToken = crypto.randomBytes(32).toString('hex') + await db.query( + `UPDATE users SET confirmation_token = $1, confirmation_sent_at = NOW() WHERE id = $2`, + [confirmToken, user.id], + ) + + try { + await sendConfirmationEmail(email, user.name as string, confirmToken) + } catch (err) { + console.error('[email] Resend confirmation failed:', err) + } + + return reply.code(200).send({ ok: true }) + }, + ) + // ── POST /api/auth/refresh ───────────────────────────────────────────────── fastify.post('/api/auth/refresh', async (request, reply) => { const refreshToken = request.cookies?.refresh_token diff --git a/src/lib/api.ts b/src/lib/api.ts index 77b104f..4ca9566 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -161,6 +161,9 @@ export const api = { resetPassword: (token: string, password: string) => req<{ ok: boolean }>('POST', '/api/auth/reset-password', { token, password }), + + resendConfirmation: (email: string) => + req<{ ok: boolean }>('POST', '/api/auth/resend-confirmation', { email }), }, // ── Rooms ───────────────────────────────────────────────────────────────── diff --git a/src/pages/LoginPage.tsx b/src/pages/LoginPage.tsx index 06212ac..fb0e772 100644 --- a/src/pages/LoginPage.tsx +++ b/src/pages/LoginPage.tsx @@ -7,7 +7,7 @@ import { import { useAuth } from '../contexts/AuthContext' import { useTheme } from '../contexts/ThemeContext' import { cn } from '../lib/utils' -import { api } from '../lib/api' +import { api, ApiError } from '../lib/api' const DEMO_EMAILS = [ 'manager@grand-palace.ru', @@ -51,11 +51,14 @@ function LoginForm({ onSwitch, onForgot }: { onSwitch: () => void; onForgot: () const { login } = useAuth() const navigate = useNavigate() - const [email, setEmail] = useState('') - const [password, setPassword] = useState('') - const [showPass, setShowPass] = useState(false) - const [loading, setLoading] = useState(false) - const [error, setError] = useState('') + const [email, setEmail] = useState('') + const [password, setPassword] = useState('') + const [showPass, setShowPass] = useState(false) + const [loading, setLoading] = useState(false) + const [error, setError] = useState('') + const [unconfirmedEmail, setUnconfirmedEmail] = useState(null) + const [resendLoading, setResendLoading] = useState(false) + const [resendDone, setResendDone] = useState(false) const [searchParams] = useSearchParams() const confirmed = searchParams.get('confirmed') === '1' const resetDone = searchParams.get('reset') === 'success' @@ -70,14 +73,29 @@ function LoginForm({ onSwitch, onForgot }: { onSwitch: () => void; onForgot: () if (!loggedIn) { setError('Неверный email или пароль'); return } navigate('/calendar') } catch (err) { - const msg = err instanceof Error ? err.message : 'Ошибка соединения' - // Strip "Ошибка сервера:" prefix for clean 403 messages from backend - setError(msg) + if (err instanceof ApiError && err.status === 403) { + setUnconfirmedEmail(email) + setResendDone(false) + } else { + const msg = err instanceof Error ? err.message : 'Ошибка соединения' + setError(msg) + } } finally { setLoading(false) } } + const handleResend = async () => { + if (!unconfirmedEmail) return + setResendLoading(true) + try { + await api.auth.resendConfirmation(unconfirmedEmail) + setResendDone(true) + } catch { /* silent */ } finally { + setResendLoading(false) + } + } + return ( <>

@@ -93,7 +111,7 @@ function LoginForm({ onSwitch, onForgot }: { onSwitch: () => void; onForgot: () { setEmail(e.target.value); setError('') }} + onChange={e => { setEmail(e.target.value); setError(''); setUnconfirmedEmail(null) }} required /> @@ -112,7 +130,7 @@ function LoginForm({ onSwitch, onForgot }: { onSwitch: () => void; onForgot: () { setPassword(e.target.value); setError('') }} + onChange={e => { setPassword(e.target.value); setError(''); setUnconfirmedEmail(null) }} required /> + )} + + )} + {confirmed && (
Email подтверждён! Теперь вы можете войти.