R
R
rusline2017-10-28 18:31:33
Node.js
rusline, 2017-10-28 18:31:33

How did those specified in the settings output errors in the passport-local-mongoose module?

I have a question, I did authorization using this passport-local-mongoose module and there was such a problem that I can’t figure out how to make the errors that I specified in the options of the module display them, and didn’t I write these errors myself in the message?

router.post('/', (req, res) => {
  let authenticate = User.authenticate();
  authenticate(req.body.username, req.body.password, (err, result) => {
    if (err) {
      console.log(err);
      return res.render('index', {message: err})
    }
    if (result === false){
            return res.render('index', {message: 'Неверный логи или пароль'})
    }
        passport.authenticate('local')(req, res, function () {
            res.redirect('/users');
        });
  })
});

And in the model I specified the plugin connection
let mongoose = require('../bin/mongoose'),
  passportLocalMongoose = require('passport-local-mongoose'),
  Schema = mongoose.Schema;

let schema = new Schema({
  username: {
    type: String,
  },
  password: {
    type: String,
  },
  created: {
    type: Date,
    default: Date.now
  }
});

schema.plugin(passportLocalMongoose, {
  errorMessages: {
    UserExistsError: 'Данный пользователь уже зарегестрировался',
        IncorrectPasswordError: 'Неправильный логин или пароль',
        IncorrectUsernameError: 'Неправильный логин или пароль'
    }
});

module.exports = mongoose.model('User', schema);

That's what I didn't have so that it would display an IncorrectPasswordError error, and not result false. At least I didn't find any answers. Can someone help me figure it out

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question