Reputation: 437
We are building an API that consumes multiple API's (to make it easier for students and fellow researcher to download data) and as things are progressing the amount of Routers and Strategies are building up. To avoid reusing the same code and changing one name in the link we thought to use the req.params to make a universal authenticationRouter to handle the general authentication through Oauth2 of users granting us access to their information. The issue we are running into is how to parse the req.params into the passport.authenticate function to use the specific strategy. Ideally we would like the strategy to be universal as well by loading the API-specific settings into the Strategy.
With an API-specific router we use the following, taking fitbit as an example API: (leaving out the other routes)
fitbitRouter.get('/fitbit/auth', passport.authenticate('fitbit'))
fitbitRouter.get('/fitbit/auth/callback, passport.authenticate('fitbit', {
successRedirect: '/fitbit/success',
failureRedirect: '/fitbit/failure'
}
What we would like to get is:
authRouter.get('/:provider/auth', passport.authenticate(req.params.provider))
authRouter.get('/:provider/auth/callback, passport.authenticate(req.params.provider, {
successRedirect: '/fitbit/success',
failureRedirect: '/fitbit/failure'
}
I know the req is not available for the callback to passport.authenticate() but the following doesn't work either:
authRouter.get('/:provider/auth', getProvider)
authRouter.get('/:provider/auth/callback', getProviderandRedirect)
function getProvider(req, res){
var provider = req.params.provider
passport.authenticate(provider)
}
function getProviderandRedirect(req, res){
var provider = req.params.provider
passport.authenticate(provider, {
successRedirect: '/'+provider+'/success',
failureRedirect: '/'+provider+'/failure'
})
}
So the questions are
Is it possible to create this functionality of using the req.params as a String in passport.authenticate() to choose what strategy to use?
Can we extend this to parsing the req.params variable into passport.authenticate(), perhaps as an option?, to the Strategy where we load the correct settings from a dictionary, or something else, specific to that api?
For a bit of context we started with 3 API's but now we are close to 20 already and we would like to increase this whenever someone requests to include another API. Any insight is appreciated!
Upvotes: 2
Views: 1298
Reputation: 2935
passport.authenticate
returns a middleware function. you'll have to call it if you want to use it from within your own middleware (and make your function a middleware as well).
authRouter.get('/:provider/auth', getProvider)
authRouter.get('/:provider/auth/callback', getProviderandRedirect)
function getProvider(req, res, next){
var provider = req.params.provider
passport.authenticate(provider)(req, res, next)
}
function getProviderandRedirect(req, res, next){
var provider = req.params.provider
passport.authenticate(provider, {
successRedirect: '/'+provider+'/success',
failureRedirect: '/'+provider+'/failure'
})(req, res, next)
}
Upvotes: 3