GET /api/auth/me отвечает 200 с user: null для гостя
Раньше был 401, из-за чего в консоли браузера у гостя появлялась ошибка (Lighthouse Best Practices: errors-in-console). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e034da2458
commit
d4b0f881a4
@@ -1,7 +1,7 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import rateLimit from 'express-rate-limit';
|
import rateLimit from 'express-rate-limit';
|
||||||
import { z } from 'zod';
|
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 { prisma } from '../db.js';
|
||||||
import { HttpError } from '../http.js';
|
import { HttpError } from '../http.js';
|
||||||
import { hashPassword, verifyPassword } from '../services/password.js';
|
import { hashPassword, verifyPassword } from '../services/password.js';
|
||||||
@@ -42,8 +42,10 @@ authRouter.post('/logout', async (req, res) => {
|
|||||||
res.status(204).end();
|
res.status(204).end();
|
||||||
});
|
});
|
||||||
|
|
||||||
authRouter.get('/me', requireAuth, (req, res) => {
|
// Гостю отвечаем 200 и user: null (а не 401): это обычная ситуация, а не ошибка,
|
||||||
res.json({ user: toUserDto(req.user!) });
|
// и в консоли браузера не появляется красная строка при каждом открытии сайта.
|
||||||
|
authRouter.get('/me', (req, res) => {
|
||||||
|
res.json({ user: req.user ? toUserDto(req.user) : null });
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---------- Приглашения ----------
|
// ---------- Приглашения ----------
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export function useAuth() {
|
|||||||
queryKey: ['me'],
|
queryKey: ['me'],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
try {
|
try {
|
||||||
const { user } = await api<{ user: UserDto }>('/auth/me');
|
const { user } = await api<{ user: UserDto | null }>('/auth/me');
|
||||||
return user;
|
return user;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof ApiError && e.status === 401) return null;
|
if (e instanceof ApiError && e.status === 401) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user