Vizzuality/landgriffon

View on GitHub
api/src/guards/jwt-auth.guard.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
93%
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;
  }
}