forked from Shared/esg
initial commit
This commit is contained in:
21
src/auth/guards/jwt-auth.guard.ts
Normal file
21
src/auth/guards/jwt-auth.guard.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { JWT_ACCESS_STRATEGY } from '../constants/auth.constants';
|
||||
|
||||
/**
|
||||
* Protects routes with Passport JWT access strategy.
|
||||
*/
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard(JWT_ACCESS_STRATEGY) {
|
||||
override handleRequest<TUser>(
|
||||
err: Error | null,
|
||||
user: TUser | false,
|
||||
_info: unknown,
|
||||
_context: ExecutionContext,
|
||||
): TUser {
|
||||
if (err || !user) {
|
||||
throw err ?? new UnauthorizedException('Unauthorized');
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user