whitecloakph / nestjs-passport-firebase

Passport - Firebase Auth Module for NestJS
MIT License
42 stars 4 forks source link

Bypass global guard #13

Open jerearaujo03 opened 11 months ago

jerearaujo03 commented 11 months ago

In my app.module.ts I have set up a global guard because I want to protect almost all my routes.

providers: [
    FirebaseCustomStrategy,
    {
      provide: APP_GUARD,
      useClass: FirebaseAuthGuard,
    },
]

But the problem is that some routes are public. How can I "bypass" the guard for specific routes? Is there a way to set a specific metadata to avoid the guard?

jerearaujo03 commented 11 months ago

I managed to solve by doing this:

import { ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { FirebaseAuthGuard } from '@whitecloak/nestjs-passport-firebase';
import { IS_PUBLIC } from './auth.decorator';

@Injectable()
export class CustomFirebaseAuthGuard extends FirebaseAuthGuard {
  constructor(private reflector: Reflector) {
    super();
  }

  canActivate(context: ExecutionContext) {
    const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC, [
      context.getHandler(),
      context.getClass(),
    ]);
    if (isPublic) {
      return true;
    }
    return super.canActivate(context);
  }
}
jimuelpalaca commented 11 months ago

HI @jerearaujo03, glad you make it work via the custom guard. If you think it would be nice to have such feature, we're happy to accept a PR.