From b2298e0fb1c03672bf1191a9e6330b98208e568f Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Fri, 14 Nov 2025 16:58:24 +0530 Subject: [PATCH] fixed the middleware functions --- src/common/middlewares/jwt/authForMinglarAdmin.ts | 11 +++++++---- src/common/middlewares/jwt/authForUser.ts | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/common/middlewares/jwt/authForMinglarAdmin.ts b/src/common/middlewares/jwt/authForMinglarAdmin.ts index f354b16..3be0ded 100644 --- a/src/common/middlewares/jwt/authForMinglarAdmin.ts +++ b/src/common/middlewares/jwt/authForMinglarAdmin.ts @@ -9,7 +9,8 @@ import { ROLE } from '@/common/utils/constants/common.constant'; const prisma = new PrismaClient(); interface DecodedToken { - id: number; + id?: number; + sub?: string | number; role?: string; iat: number; exp: number; @@ -36,15 +37,17 @@ export async function verifyMinglarAdminToken(token: string): Promise<{ id: numb } try { - const decoded = jwt.verify(token, config.jwt.secret) as DecodedToken; + const decoded = jwt.verify(token, config.jwt.secret) as unknown as DecodedToken; - if (!decoded?.id) { + const userId = decoded.id ?? (decoded.sub ? Number(decoded.sub) : null); + + if (!userId) { throw new ApiError(httpStatus.UNAUTHORIZED, 'Invalid token payload'); } // ✅ Fetch user from Prisma (Host user only) const user = await prisma.user.findUnique({ - where: { id: decoded.id }, + where: { id: userId }, include: { role: true }, }); diff --git a/src/common/middlewares/jwt/authForUser.ts b/src/common/middlewares/jwt/authForUser.ts index d24c5f4..2b20c03 100644 --- a/src/common/middlewares/jwt/authForUser.ts +++ b/src/common/middlewares/jwt/authForUser.ts @@ -9,7 +9,8 @@ import { ROLE } from '@/common/utils/constants/common.constant'; const prisma = new PrismaClient(); interface DecodedToken { - id: number; + id?: number; + sub?: string | number; role?: string; iat: number; exp: number; @@ -36,15 +37,17 @@ export async function verifyUserToken(token: string): Promise<{ id: number; role } try { - const decoded = jwt.verify(token, config.jwt.secret) as DecodedToken; + const decoded = jwt.verify(token, config.jwt.secret) as unknown as DecodedToken; - if (!decoded?.id) { + const userId = decoded.id ?? (decoded.sub ? Number(decoded.sub) : null); + + if (!userId) { throw new ApiError(httpStatus.UNAUTHORIZED, 'Invalid token payload'); } // ✅ Fetch user from Prisma (Host user only) const user = await prisma.user.findUnique({ - where: { id: decoded.id }, + where: { id: userId }, include: { role: true }, });