问题
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