How to add error handling with Express Passport in Node.js?

Sometimes, we want to add error handling with Express Passport in Node.js.

In this article, we’ll look at how to add error handling with Express Passport in Node.js.

How to add error handling with Express Passport in Node.js?

To add error handling with Express Passport in Node.js, we can set the failureRedirect and failureFlash options.

For instance, we write

app.post('/login', passport.authenticate('local', {
  successRedirect: '/loggedin',
  failureRedirect: '/login',
  failureFlash: true
}));

to set failureRedirect to '/login' to redirect to the /login route when login failed.

When failureFlash is set to true, we can store the message we call next with if the connect-flash middleware is installed.

Conclusion

To add error handling with Express Passport in Node.js, we can set the failureRedirect and failureFlash options.