how to send json as a response after passport authenticationin node.js how to send json as a response after passport authenticationin node.js express express

how to send json as a response after passport authenticationin node.js


You can use passport's authenticate function as route middleware in your express application.

app.post('/login',  passport.authenticate('local'),  function(req, res) {    // If this function gets called, authentication was successful.    // `req.user` contains the authenticated user.    // Then you can send your json as response.    res.json({message:"Success", username: req.user.username});  });

By default, if authentication fails, Passport will respond with a 401 Unauthorized status, and any additional route handlers will not be invoked. If authentication succeeds, the next handler will be invoked and the req.user property will be set to the authenticated user.


here I modified my code to send json as a response

// process the signup formapp.post('/signup', passport.authenticate('local-signup', {    successRedirect : '/successjson', // redirect to the secure profile section    failureRedirect : '/failurejson', // redirect back to the signup page if there is an error    failureFlash : true // allow flash messages}));app.get('/successjson', function(req, res) {    res.sendfile('public/index.htm');});app.get('/failurejson', function(req, res) {    res.json({ message: 'hello' });});


Create new route, e.g.: /jsonSend with res.json in it and make successRedirect: '/jsonSend'. That should do it.