node.jsexpresspassport.jsmiddlewarepassport-google-oauth2

How to process passport authenticate function in as request handler function


I am creating a web application based on Node.js and Express 4. I am also using Passportjs and Google OAuth 2 Startegy for authentication.

I am trying to configure my routes in order to process requests. I have learned that this line work well:

router.get('/signin/google/callback', passport.authenticate('google', {failureRedirect: '/signin'}));

but when I decided to handle the route in the function, application stopped responding:

router.get('/signin/google/callback', function (req, res) {
    passport.authenticate('google', {failureRedirect: '/signin'});
});

Do I miss something? thanks in advance


Solution

  • The callback of the Google OAuth function should be something like this:

    app.get('/auth/google/callback', 
      passport.authenticate('google', { failureRedirect: '/login' }),
      function(req, res) {
        res.redirect('/');
      });
    

    The passport.authenticate() is middleware with arguments request,response, next. You could also define your own middlewares or the last request handler.