NestJS Multitenancy - Using Passport Strategies with Request Scope

Published on

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); … }

comments powered by Disqus