首页 > 解决方案 > 更改后重新处理请求

问题描述

我正在尝试以passport.js通用方式使用,在调用之前更改配置authenticate,但之后我找不到将请求重定向到它的方法。

我处理这样的请求:

入口点

app.get('/authorize/:clientId/:network', authUtils.authorize);

处理程序

async function authorize(request, response, next) {
  try {
    let clientId = request.params.clientId;
    let network = request.params.network;

    config = await databaseUtils.getConfig(clientId, network);

    if(typeof(config) !== 'undefined') {
      passport.use(new FacebookStrategy({
        clientID: config.appId,
        clientSecret: config.appSecretKey,
        callbackURL: 'https://127.0.0.1/' + network + '/authCallback/'
      }, function(accessToken, refreshToken, profile, cb) {
        return {accessToken, refreshToken, profile, cb};
      }));

      // Here's where I should re-handle request
      passport.authenticate(network);
    } else {
      throw new Error(network + ' is not configured');
    }
  } catch (e) {
    throw e;
  }
}

我确定使用passport.authenticate('network')as request handler 是可行的,但我想让它通用,这样我就可以使用不同的社交网络。

有没有办法在 NodeJS/Express 中“重新处理”请求?

标签: node.jsexpresspassport.jshandler

解决方案


由于中间件的所有步骤都应该返回一个带有 3 个参数的函数,也许您可​​以使用这 3 个参数调用它返回的内容:

return passport.authenticate(network)(request, response, next);


推荐阅读