forked from Shared/esg
22 lines
604 B
TypeScript
22 lines
604 B
TypeScript
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;
|
|
}
|
|
}
|