we are building a multi-tenant application using NestJS and we have faced what i think is a common issue since the underlying problem have been posted in this github issue
https://github.com/nestjs/nest/issues/1870
the application throws the following Error at runtime and to be specific when calling a rest API
Error: Unknown authentication strategy "jwt"
we have created this MongooseConfigService that configures mongoose with specific tenant database on every call
@Injectable({ scope: Scope.DEFAULT })
export class MongooseConfigService implements MongooseOptionsFactory {
private mongooseConnectionManager: MongooseConnectionManager
constructor(
@Inject(REQUEST) private readonly request: IRequest,
) {
this.mongooseConnectionManager = MongooseConnectionManager.getInstance()
}
createMongooseOptions(): MongooseModuleOptions {
const tenantInfo = this.request.tenantInfo;
let connection:mongoose.Connection = undefined;
let uri:string = undefined
if(tenantInfo.tenantId){
uri = `...`;
connection = this.mongooseConnectionManager.getTenantConnection(tenantInfo.tenantId, uri);
}else{
uri = `...`;
connection = this.mongooseConnectionManager.getGlobalConnection(uri);
}
return {
uri,
connectionFactory: () => connection,
};
}
}
this works well until we integrate JWT authentication using passport
@Module({
imports: [
EventEmitterModule.forRoot(),
ConfigModule.forRoot({
isGlobal:true,
load:[Configuration]
}),
MongooseModule.forRootAsync({
useClass: MongooseConfigService,
}),
UsersModule,
AuthModule,
TenantModule
],
providers:[
{
provide: APP_GUARD,
useClass: JwtAuthGuard
}
]
})
export class AppModule {}
Any help would be highly appreciated