-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(auth): process env reading in other module & refactored jwt guard
- Loading branch information
Showing
3 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,51 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ExceptionService } from './../../../../shared/exception.service'; | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Request } from 'express'; | ||
import { AuthExpectionKeys, ExceptionStatusKeys } from 'src/enums'; | ||
|
||
@Injectable() | ||
export class JwtGuard extends AuthGuard('jwt') {} | ||
export class JwtGuard implements CanActivate { | ||
constructor( | ||
private jwtService: JwtService, | ||
private exceptionService: ExceptionService, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const token = this.extractTokenFromHeader(request); | ||
if (!token) { | ||
this.exceptionService.throwError( | ||
ExceptionStatusKeys.BadRequest, | ||
'Token not found', | ||
AuthExpectionKeys.TokenNotFound, | ||
); | ||
} | ||
try { | ||
const payload = await this.jwtService.verifyAsync(token, { | ||
secret: `${process.env.JWT_SECRET}`, | ||
}); | ||
request['user'] = payload; | ||
} catch (err) { | ||
this.exceptionService.throwError( | ||
ExceptionStatusKeys.BadRequest, | ||
'Invalid token', | ||
AuthExpectionKeys.TokenInvalid, | ||
); | ||
} | ||
return true; | ||
} | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const authHeader = request.headers['authorization']; | ||
const accessTokenCookie = request.cookies.access_token; | ||
let accessToken: string; | ||
|
||
if (authHeader && authHeader.startsWith('Bearer ')) { | ||
accessToken = authHeader.substring(7); | ||
} else if (accessTokenCookie) { | ||
accessToken = accessTokenCookie; | ||
} | ||
return accessToken; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters