api/src/guards/jwt-auth.guard.ts
import {
Injectable,
ExecutionContext,
UnauthorizedException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Reflector } from '@nestjs/core';
import { Observable } from 'rxjs';
import { User } from 'modules/users/user.entity';
import * as config from 'config';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
constructor(private readonly reflector: Reflector) {
super();
}
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const isPublic: boolean = this.reflector.get<boolean>(
'isPublic',
context.getHandler(),
);
if (isPublic) {
return true;
}
return super.canActivate(context);
}
handleRequest(err: Error, user: User, info: string): any {
if (err || !user) {
if (`${config.get('auth.requireUserAuth')}`.toLowerCase() === 'false') {
return null;
}
throw err || new UnauthorizedException();
}
return user;
}
}