Questions & Answers

Password validation not working in my passport local mongoose

I am learning passportjs using the module "passport-local-mongoose". But the password validation keeps failing.

I configured the errorMessages option, which allows you to specify custom error messages for different validation errors that might occur. I also made sure that I included a custom error message for the ValidatorError error, which is supposed to be thrown when the validator function returns false. but nothing seems to work and I don't know where my error is coming from.

//This is my model

const mongoose = require('mongoose')
const passportLocalMongoose = require('passport-local-mongoose')

const userSchema = mongoose.Schema({
  //Provide more schemas if you desire.

userSchema.plugin(passportLocalMongoose, {
    usernameField: 'email',
    validator: function(password) {
        return password.length >= 8 && /[A-Z]/.test(password) && /[a-z]/.test(password) && /[0-9]/.test(password);
    errorMessages: {
        saltlen : 10, //length of salt
        UserExistsError: 'The email you provided is already in use...',
        IncorrectPasswordError : 'Incorrect Password...',
        IncorrectUsernameError : 'Email does not exist...',
        MissingUsernameError: 'Please provide your email...',
        ValidatorError: 'Your password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, and one number...',
        MissingPasswordError: 'Please enter your password...'
      limitAttempts : true,
      maxAttempts : 5,
      unlockInterval : 20000, //period in which a locked account is deactivated. 20 seconds here.

module.exports = mongoose.model('User', userSchema)

//This is my registration route
exports.register = async (req, res, next) => {
    try {
        const user = await User.register(
            { email: }, req.body.password)
      } catch (error) {
        res.render('register', { error : error.message });
//This is my app.js


2023-01-07 20:33:54
1. Code should be added as text. 2. What is User.register and why the first argument is an object an the second one is just a string? 3. These ... await look unsettling as if User.register didn't return a promise
2023-01-07 20:33:54
Okay. let me quickly fix that.
2023-01-07 20:33:54
There are too much call backs in the original documentation. I just converted it into async await for better redeability.
Answers(0) :