title: How to show custom error messages using passport and express
body: <p>I check if the email already exist when registering a user. If a user already exist I pass the error message "email already exists". But in the front end it shows "Unauthorized" error 401. I want to pass the error message which I pass to the front end from the back end but it pass the default message. below is how I check if a user already exist and send the error message,</p>
  
  <pre><code>exports.register = new LocalStrategy({
      usernameField: 'email',
      passReqToCallback: true
  }, function(req, email, password, done,res) {
      var newUser = new User({
          email: email,
          password: password,
          name: req.body.fname
      });
      var searchUser = {
          email: email
      };
      User.findOne(searchUser, function(err, user) {
          if (err) return done(err);
          console.log("err "+err);
  
          if (user) {
              return done(null, false, {
                  message: "email already exists"
              });
          }
          newUser.save(function(err) {
          console.log("newUser "+newUser);
              done(null, newUser);
          })
      });
  });
  </code></pre>
  
  <p>I use passport for authentication,</p>
  
  <pre><code>authRouter.post('/signup', passport.authenticate('local-register'),function(req, res) {
      createSendToken(req.user, res);
  });
  </code></pre>
  
  <p>The error message is not the one I pass to the front end. It shows unauthorized error, which is the default error. When I print the error message in the console in front end it shows,</p>
  
  <pre><code>Object {data: "Unauthorized", status: 401, config: Object, statusText: "Unauthorized"}
  </code></pre>