NestJS Multitenancy - Using Passport Strategies with Request Scope

Published on

https://github.com/nestjs/passport/issues/95

I ran into this issue when setting up multi-tenancy with NestJS. In your JwtStrategy you need to inject ModuleRef and set passReqToCallback to true.

@Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(private moduleRef: ModuleRef) { super({ passReqToCallback: true }); }

public async validate(payload: any): Promise { // Validation here } } You will use the ModuleRef to resolve any request-scoped dependencies that you need.

public async validate(request: Request, payload: any) { const contextId = ContextIdFactory.getByRequest(request); // “AuthService” is a request-scoped provider const authService = await this.moduleRef.resolve(AuthService, contextId); … } https://docs.nestjs.com/security/authentication#request-scoped-strategies https://github.com/jmw5598/nestjs-angular-multitenancy

comments powered by Disqus