更改后重新处理请求 [英] Re-handle request after changes
问题描述
我正在尝试以通用方式使用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屋!