更改后重新处理请求 [英] Re-handle request after changes

查看:84
本文介绍了更改后重新处理请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我这样处理请求:

入口点:

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')作为请求处理程序是可以的,但是我想使其通用,这样我就可以使用其他社交网络.

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.

是否可以在NodeJS/Express中重新处理"请求?

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

推荐答案

由于中间件的所有步骤都应返回带有3个参数的函数,也许您可​​以调用带有以下3个参数的函数:

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:

返回护照.authenticate(network)(请求,响应,下一个);

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

这篇关于更改后重新处理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆