feat: inject sessionId for logging purpose

This commit is contained in:
Methapon2001 2024-06-25 13:10:42 +07:00
parent e3e44d03b5
commit cd47863f2a
2 changed files with 10 additions and 3 deletions

View file

@ -10,8 +10,14 @@ export async function expressAuthentication(
) {
switch (securityName) {
case "keycloak":
return keycloakAuth(request, scopes);
const authData = await keycloakAuth(request, scopes);
request.app.locals.logData.sessionId = authData.session_state;
return authData;
default:
throw new HttpError(HttpStatus.NOT_IMPLEMENTED, "Unknown how to verify identity.", "unknowHowToVerify");
throw new HttpError(
HttpStatus.NOT_IMPLEMENTED,
"Unknown how to verify identity.",
"unknowHowToVerify",
);
}
}

View file

@ -1,5 +1,6 @@
import { NextFunction, Request, Response } from "express";
import elasticsearch from "../services/elasticsearch";
import { randomUUID } from "crypto";
if (!process.env.ELASTICSEARCH_INDEX) {
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
@ -50,7 +51,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
host: req.hostname,
sessionId: req.headers["x-session-id"],
rtId: req.headers["x-rtid"],
tId: req.headers["x-tid"],
tId: randomUUID(),
method: req.method,
endpoint: req.url,
responseCode: res.statusCode,