0

I have a JWT validate middleware like bellow

app.module.ts

@Module({
  imports: [
    MongooseModule.forRoot(global.SkeletonConfig.MONGODB_URI),
    UserModule,
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
    JwtService,
  ],
})
export class AppModule {}

And the User module

user.module.ts
@Module({
  imports: [MongooseModule.forFeatureAsync([mongooseModuleFunction(User)])],
  controllers: [UserController],
  providers: [UserService, User],
})
mongooseModuleFunction.ts
export function mongooseModuleFunction(schema: Schema<any>) {
  return {
    name: schema.name,
    useFactory: mongooseFactory(schema),
    inject: [REQUEST],
  }
}
export function mongooseFactory(schema: Schema<any>) {
  return async (req: Request) => {
    

    schema.set('collection', req.collectionName)
    return UserSchema
  }
}

When I call a request, NestJS will run the MongooseModule.forFeatureAsync([mongooseModuleFunction(User)]) before the AuthGuard

How can I change this behavior? I need to parse the JWT payload first and put it into the request to identify which collection will be used for each request.

vy.pham
  • 571
  • 6
  • 20

0 Answers0