#StackBounty: #nestjs Nestjs passport authentication with multiple strategies

Bounty: 50

I have multiple authentication strategies, example for one of them:

@Injectable()
export class EmployeeStrategy extends PassportStrategy(Strategy, 'employee') {
  constructor(
    private authService: AuthService,
    @Inject(appConfig.KEY)
    configService: ConfigType<typeof appConfig>,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: configService.EMPLOYEE_KEY,
    });
  }

  async validate({ phone }: JwtPayload) {
    const employee = await this.authService.authByRole(phone, Role.Employee);

    if (!employee) {
      throw new UnauthorizedException('insufficient scope');
    }

    return employee;
  }

And some others mostly like this one. But because i throw unauthorized exception inside it, i cannot use multiple of them at the same route/controller. E.g.

  @UseGuards(AuthGuard(['employee', 'admin']))

The first one that crashes leading to error. How to solve that problem?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.