Answers for "Pass parameter to NestJs Guard"

0

Pass parameter to NestJs Guard

export const RoleGuard = (role: string) => {
  class RoleGuardMixin implements CanActivate {
    canActivate(context: ExecutionContext) {
      // do something with context and role
      return true;
    }
  }

  const guard = mixin(RoleGuardMixin);
  return guard;
}
// mixin as a function is imported from @nestjs/common and is a wrapper function that applies the @Injectable() decorator to the class

// Now to use the guard, you need to do something like 
@UseGuards(RoleGuard('admin'))
Posted by: Guest on January-30-2022

Code answers related to "Pass parameter to NestJs Guard"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language