PassportJS with Swagger-Node

1,387 views
Skip to first unread message

Sean Michael

unread,
Feb 1, 2016, 12:03:39 AM2/1/16
to Swagger
Hey Group,

I'm having quite a difficult time figuring out how to incorporate passport.js into a swagger-node project.  It doesn't help that I'm not yet an expert at Node, Express, or Swagger, which is what I'm using to build my API.  I was able to set up registration and login endpoints for Node and Express on a previous app utilizing passport.js.  On this project I was not using Swagger, but I was able to successfully set up auth with JWT's.  This is what I would like to do on this project as well.  Here are the Express endpoints:
app.post('/register', passport.authenticate('local-register'), function(req,res) {
   createSendToken
(req.user, res);
});
app.post('/login', passport.authenticate('local-login'), function(req, res) {
   createSendToken
(req.user, res);
});

In this specific instance I was using createSendToken to respond with a JSON web token.  I also have set up two different local authentication strategies which I register with the `passport.use` method before these routes ('local-login' and 'local-register').   These two details most likely don't affect the way I need to implement passport.js with Swagger, but thought I'd add them to the post.

The difficulty I'm running into is that when I set up an endpoint in my Swagger yaml file, I don't know where or how to implement my passport authentication strategies.  I tried setting them up in the controller where the endpoint is wired up, but it still doesn't allow me to add my passport strategy before the method for the route is called.  I tried incorporating this in various ways into the route's controller and was not successful.  Then I also tried adding it in a standard Express route in `app.js` before Swagger was started up and those just seemed to conflict with the Swagger routes.  Should I remove the routes from Swagger and include them like I normally would with Express/passport.js in the `app.js` file?  

I have a feeling there is a way around this without having to completely bypass Swagger.  Any ideas?

Thanks,
Sean

Jeremy Whitlock

unread,
Feb 1, 2016, 5:00:00 PM2/1/16
to Swagger
swagger-node uses swagger-tools and it has an open issue to make this simpler: https://github.com/apigee-127/swagger-tools/issues/203. It also has examples of people doing this successfully right now so it might help you.

Sean Michael

unread,
Feb 1, 2016, 5:10:43 PM2/1/16
to Swagger
We meet again :-).  I actually had commented on that open ticket, but didn't want to continue asking my novice questions there because the thread was for a feature in swagger-tools, rather than a how-to guide.  The comments weren't enough to help me understand where and how to implement PassportJS with swagger-tools.  However, it looks like someone posted there code just a few hours ago.  I haven't taken a look at this code yet, but maybe it will fill me in enough about where and how I need to set up the auth strategies.  If you have any other suggestions, feel free to let me know.
Reply all
Reply to author
Forward
0 new messages