当我在承诺中从数据库传递选定路线时,自动化不起作用。这意味着传递路由的请求总是被授权。
protected applyRoutes(consumer: MiddlewaresConsumer) {
let paths = this.authPathService.findAll();
paths.then((resultPaths) => {
let result: {}[] = [];
for (let path of resultPaths) {
result.push({
path: path.path,
method: RequestMethod.ALL
})
}
consumer
.apply(passport.authenticate('jwt', { session: false }))
.forRoutes(...result);
return result;
}, (error) => {
console.log('error', error);
});
}
当我在对象数组中传递路径时,它运行良好
protected applyRoutes(consumer: MiddlewaresConsumer) {
consumer
.apply(passport.authenticate('jwt', { session: false }))
.forRoutes(...[
{ path: '/auth/authorized', method: RequestMethod.ALL },
{ path: '/auth/test', method: RequestMethod.ALL }]);
}
答案 0 :(得分:1)
使用MiddlewaresConsumer
异步应用中间件是不可能的。相反,注册将获取所有路径的异步组件(https://docs.nestjs.com/fundamentals/async-components),例如AUTH_PATHS
,然后将其注入模块类,假设AuthModule
并在{{1}内使用此数组1}}方法。