From 6a97eb8003245593045633f70e09f2a867c32ffa Mon Sep 17 00:00:00 2001 From: pratik Date: Thu, 26 Sep 2024 15:19:39 +0530 Subject: [PATCH] send 400 bad request error for no user-id header in api request --- .../src/api/middlewares/UserCheckMiddleware.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts b/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts index 69d3eee464..9ebeedf8cd 100644 --- a/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts +++ b/backend/packages/Upgrade/src/api/middlewares/UserCheckMiddleware.ts @@ -12,8 +12,16 @@ export class UserCheckMiddleware { public async use(req: AppRequest, res: AppRequest, next: express.NextFunction): Promise { try { const user_id = req.get('User-Id'); - req.logger.child({ user_id }); - req.logger.debug({ message: 'User Id is:', user_id }); + if (!user_id) { + const error = new Error(`User-Id header not found.`); + (error as any).type = SERVER_ERROR.USER_NOT_FOUND; + (error as any).httpCode = 400; + req.logger.error(error); + return next(error); + } else { + req.logger.child({ user_id }); + req.logger.debug({ message: 'User Id is:', user_id }); + } const experimentUserDoc = await this.experimentUserService.getUserDoc(user_id, req.logger); if (!req.url.endsWith('/init') && !experimentUserDoc) {