3

各リクエストをチェックするための認証ミドルウェアを作成しました。ミドルウェアはサーバーを使用しています (req.connection にデータが見つからなかった場合のみ)。ミドルウェアにサービスを挿入しようとしているのですが、同じエラーが表示され続けます。

認証モジュール:

@Module({
   imports: [ServerModule],
   controllers: [AuthenticationMiddleware],
})
export class AuthenticationModule {
}

認証ミドルウェア:

@Injectable()
export class AuthenticationMiddleware implements NestMiddleware {

constructor(private readonly service : UserService) {}

resolve(): (req, res, next) => void {
 return (req, res, next) => {
   if (req.connection.user)
    next();

  this.service.getUsersPermissions()     
  }
}

サーバーモジュール:

@Module({
 components: [ServerService],
 controllers: [ServerController],
 exports: [ServerService]
})    
 export class ServerModule {}

アプリケーションモジュール:

@Module({
  imports: [
    CompanyModule,
    ServerModule,
    AuthenticationModule
  ]
})

export class ApplicationModule implements NestModule{
  configure(consumer: MiddlewaresConsumer): void {
  consumer.apply(AuthenticationMiddleware).forRoutes(
      { path: '/**', method: RequestMethod.ALL }
   );
 }
}
4

1 に答える 1

8

あなたのアプリケーションはAuthMiddleware依存関係を解決できUserServiceません. したがって、作成する必要があるのは次のとおりです。ServerModuleAuthenticationModuleServerService

@Injectable()
export class AuthenticationMiddleware implements NestMiddleware {

  constructor(private readonly service : ServerService) {}

  resolve(): (req, res, next) => void {
    return (req, res, next) => {
      if (req.connection.user)
        next();

    this.service.getUsersPermissions()     
  }
}

NestJS 依存関係コンテナーの詳細については、こちらを参照してください。

于 2018-02-07T21:56:29.407 に答える