diff --git a/src/app.ts b/src/app.ts index 1a4a6a63..faa5d248 100644 --- a/src/app.ts +++ b/src/app.ts @@ -26,6 +26,7 @@ import Config from "./common/config"; import database from "./middleware/database"; import corsSelector from "./middleware/cors-selector"; import { getOpenAPISpec, SWAGGER_UI_OPTIONS } from "./common/openapi"; +import { tryGetAuthenticatedUser } from "./common/auth"; const app = express(); @@ -34,7 +35,10 @@ app.use(corsSelector); // Enable request output when not a test if (!Config.TEST) { - app.use(morgan("dev")); + morgan.token("id", function (req, _res) { + return tryGetAuthenticatedUser(req)?.id || "unauthenticated"; + }); + app.use(morgan(":status :method :url :id :response-time ms")); } // Automatically convert requests from json diff --git a/src/common/auth.ts b/src/common/auth.ts index 60d15434..d65eb58f 100644 --- a/src/common/auth.ts +++ b/src/common/auth.ts @@ -1,6 +1,5 @@ import ms from "ms"; import jsonwebtoken, { SignOptions } from "jsonwebtoken"; -import { Request } from "express"; import { RequestHandler } from "express-serve-static-core"; import passport, { AuthenticateOptions, Profile } from "passport"; @@ -164,7 +163,7 @@ export function decodeJwtToken(token?: string): JwtPayload { * @param req The request * @returns User payload */ -export function getAuthenticatedUser(req: Request): JwtPayload { +export function getAuthenticatedUser(req: IncomingMessage): JwtPayload { return decodeJwtToken(req.headers.authorization); } @@ -173,7 +172,7 @@ export function getAuthenticatedUser(req: Request): JwtPayload { * @param req The request * @returns User payload */ -export function tryGetAuthenticatedUser(req: Request): JwtPayload | null { +export function tryGetAuthenticatedUser(req: IncomingMessage): JwtPayload | null { try { return decodeJwtToken(req.headers.authorization); } catch {