@@ -19,6 +19,7 @@ import jwt from 'jsonwebtoken';
1919import Sentry from '@sentry/minimal' ;
2020import { Encryptor } from '../helpers/encryption/encryptor.js' ;
2121import { EncryptionAlgorithmEnum } from '../enums/encryption-algorithm.enum.js' ;
22+ import { IRequestWithCognitoInfo } from './cognito-decoded.interface.js' ;
2223
2324@Injectable ( )
2425export class AuthWithApiMiddleware implements NestMiddleware {
@@ -27,7 +28,7 @@ export class AuthWithApiMiddleware implements NestMiddleware {
2728 private readonly userRepository : Repository < UserEntity > ,
2829 ) { }
2930
30- async use ( req : Request , _res : Response , next : ( err ?: any , res ?: any ) => void ) : Promise < void > {
31+ async use ( req : IRequestWithCognitoInfo , _res : Response , next : ( err ?: any , res ?: any ) => void ) : Promise < void > {
3132 try {
3233 await this . authenticateRequest ( req ) ;
3334 next ( ) ;
@@ -37,7 +38,7 @@ export class AuthWithApiMiddleware implements NestMiddleware {
3738 }
3839 }
3940
40- private async authenticateRequest ( req : Request ) : Promise < void > {
41+ private async authenticateRequest ( req : IRequestWithCognitoInfo ) : Promise < void > {
4142 const tokenFromCookie = this . getTokenFromCookie ( req ) ;
4243 if ( tokenFromCookie ) {
4344 await this . authenticateWithToken ( tokenFromCookie , req ) ;
@@ -57,10 +58,10 @@ export class AuthWithApiMiddleware implements NestMiddleware {
5758 throw new InternalServerErrorException ( Messages . AUTHORIZATION_REJECTED ) ;
5859 }
5960
60- private async authenticateWithToken ( tokenFromCookie : string , req : Request ) : Promise < void > {
61+ private async authenticateWithToken ( tokenFromCookie : string , req : IRequestWithCognitoInfo ) : Promise < void > {
6162 try {
6263 const jwtSecret = process . env . JWT_SECRET ;
63- const data = jwt . verify ( tokenFromCookie , jwtSecret ) ;
64+ const data = jwt . verify ( tokenFromCookie , jwtSecret ) as jwt . JwtPayload ;
6465 const userId = data . id ;
6566 if ( ! userId ) {
6667 throw new UnauthorizedException ( 'JWT verification failed' ) ;
@@ -88,7 +89,7 @@ export class AuthWithApiMiddleware implements NestMiddleware {
8889 }
8990 }
9091
91- private async authenticateWithApiKey ( req : Request ) : Promise < void > {
92+ private async authenticateWithApiKey ( req : IRequestWithCognitoInfo ) : Promise < void > {
9293 let apiKey = req . headers ?. [ 'x-api-key' ] ;
9394 if ( Array . isArray ( apiKey ) ) {
9495 apiKey = apiKey [ 0 ] ;
0 commit comments