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