Re-handle request after changes

…衆ロ難τιáo~ 提交于 2019-12-10 10:42:55

问题


I'm trying to use passport.js in a generic way, changing configuration before calling authenticate, but I can't find a way to redirect request to it after.

I handle request like this:

Entry point:

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

Handler:

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

I know for sure that using passport.authenticate('network') as request handler works, but I would like to make it generic so I can use different social networks.

Is there a way to "re-handle" a request in NodeJS/Express?


回答1:


As all the steps of middleware should return a function with the 3 parameters, maybe you can call what it returns with these 3 parameters:

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



来源:https://stackoverflow.com/questions/51945402/re-handle-request-after-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!