This commit is contained in:
seb
2026-10-09 10:43:56 +02:00
parent 011d84c3c1
commit 728b3ec172
3 changed files with 35 additions and 35 deletions

View File

@@ -1,7 +1,7 @@
import jwt from 'jsonwebtoken';
import env from '../../config/env.js';
import * as db from '../../db/googleAuth.js';
import { meJson } from '../auth/meJson.js';
import { respondWithAuthenticatedUser } from '../auth/meJson.js';
const userCache = new Map();
const USER_CACHE_TTL = 10 * 60 * 1000;
@@ -39,9 +39,9 @@ export function clearCachedUser(authId) {
userCache.delete(authId);
}
function authMeJson(res, req, status, body) {
function respondWithAuthPayload(res, req, status, body) {
if (req.path === '/api/auth/me') {
return meJson(res, status, body);
return respondWithAuthenticatedUser(res, status, body);
}
return res.status(status).json(body);
}
@@ -71,7 +71,7 @@ export const authMiddleware = async (req, res, next) => {
const isDocs = req.path.startsWith('/docs') || req.path.startsWith('/api/docs');
if (!token) {
if (isDocs) return next();
return authMeJson(res, req, 401, { error: 'Unauthorized' });
return respondWithAuthPayload(res, req, 401, { error: 'Unauthorized' });
}
try {
@@ -80,7 +80,7 @@ export const authMiddleware = async (req, res, next) => {
if (!Number.isInteger(authId) || authId <= 0) {
if (req.cookies.token) clearTokenCookie(res);
if (isDocs) return next();
return authMeJson(res, req, 401, { error: 'Invalid token' });
return respondWithAuthPayload(res, req, 401, { error: 'Invalid token' });
}
let user = getCachedUser(authId);
@@ -97,7 +97,7 @@ export const authMiddleware = async (req, res, next) => {
clearCachedUser(authId);
if (req.cookies.token) clearTokenCookie(res);
if (isDocs) return next();
return authMeJson(res, req, 403, { error: 'User is no longer authorized' });
return respondWithAuthPayload(res, req, 403, { error: 'User is no longer authorized' });
}
req.user = {
@@ -119,7 +119,7 @@ export const authMiddleware = async (req, res, next) => {
} catch {
if (req.cookies.token) clearTokenCookie(res);
if (isDocs) return next();
return authMeJson(res, req, 401, { error: 'Invalid token' });
return respondWithAuthPayload(res, req, 401, { error: 'Invalid token' });
}
};