NestJs / Passport身份验证对于异步传递的路由不起作用

时间:2018-01-27 13:19:58

标签: passport.js nestjs

当我在承诺中从数据库传递选定路线时,自动化不起作用。这意味着传递路由的请求总是被授权。

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

}

1 个答案:

答案 0 :(得分:1)

使用MiddlewaresConsumer异步应用中间件是不可能的。相反,注册将获取所有路径的异步组件(https://docs.nestjs.com/fundamentals/async-components),例如AUTH_PATHS,然后将其注入模块类,假设AuthModule并在{{1}内使用此数组1}}方法。