From d4b0f881a41e73039d4889c33a840314c8c39115 Mon Sep 17 00:00:00 2001 From: deonisii Date: Sun, 4 Oct 2026 13:09:04 +0300 Subject: [PATCH] =?UTF-8?q?GET=20/api/auth/me=20=D0=BE=D1=82=D0=B2=D0=B5?= =?UTF-8?q?=D1=87=D0=B0=D0=B5=D1=82=20200=20=D1=81=20user:=20null=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=B3=D0=BE=D1=81=D1=82=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Раньше был 401, из-за чего в консоли браузера у гостя появлялась ошибка (Lighthouse Best Practices: errors-in-console). Co-Authored-By: Claude Opus 5.5 --- apps/api/src/routes/auth.ts | 8 +++++--- apps/web/src/lib/auth.ts | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/api/src/routes/auth.ts b/apps/api/src/routes/auth.ts index bb99204..91b9c1a 100644 --- a/apps/api/src/routes/auth.ts +++ b/apps/api/src/routes/auth.ts @@ -1,7 +1,7 @@ import { Router } from 'express'; import rateLimit from 'express-rate-limit'; import { z } from 'zod'; -import { createSession, destroySession, requireAuth, toUserDto } from '../auth.js'; +import { createSession, destroySession, toUserDto } from '../auth.js'; import { prisma } from '../db.js'; import { HttpError } from '../http.js'; import { hashPassword, verifyPassword } from '../services/password.js'; @@ -42,8 +42,10 @@ authRouter.post('/logout', async (req, res) => { res.status(204).end(); }); -authRouter.get('/me', requireAuth, (req, res) => { - res.json({ user: toUserDto(req.user!) }); +// Гостю отвечаем 200 и user: null (а не 401): это обычная ситуация, а не ошибка, +// и в консоли браузера не появляется красная строка при каждом открытии сайта. +authRouter.get('/me', (req, res) => { + res.json({ user: req.user ? toUserDto(req.user) : null }); }); // ---------- Приглашения ---------- diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts index 01b3e51..40813bb 100644 --- a/apps/web/src/lib/auth.ts +++ b/apps/web/src/lib/auth.ts @@ -20,7 +20,7 @@ export function useAuth() { queryKey: ['me'], queryFn: async () => { try { - const { user } = await api<{ user: UserDto }>('/auth/me'); + const { user } = await api<{ user: UserDto | null }>('/auth/me'); return user; } catch (e) { if (e instanceof ApiError && e.status === 401) return null;