0

If I want to handle errors from "incorrect" logins on this route how to collect (err) and where?

// POST LOGIN
usersRouter
    .route('/login')
    .post(passport.authenticate('local'), (req, res, next) => {

        // for example, to declare an if here to catch err and call next with err...

        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        res.json({ success: true, status: 'You are successfully logged in!' });
    });
Vokz Gnakx
  • 135
  • 1
  • 7
  • 1
    please look into this [link](https://stackoverflow.com/questions/35452844/how-to-show-custom-error-messages-using-passport-and-express) – gowridev May 15 '21 at 07:33

1 Answers1

2

You can use like this

usersRouter.post('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }
    if (!user) { 
        return res.send(401,{ success : false, message : 'authentication failed' });
    }
        return res.send({ success : true, message : 'authentication succeeded' }); 
  })(req, res, next);
});
Ankita Kuchhadiya
  • 1,255
  • 6
  • 16