简体   繁体   中英

passport-local-mongoose Error message change

I want to change my Error Messages with the passport local mongoose middleware. but it didn't work:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');

var Account = new Schema({
    username: String,
    email: String
});

Account.plugin(passportLocalMongoose,{
    IncorrectUsernameError: 'sdfsd',
    IncorrectPasswordError: 'sdfsd'
});

var User = mongoose.model('Account', Account);

module.exports = User;

thats my Account.js and login / register works perfect

And my problem is, when i type in a wrong username/password the old message 'incorrect username or password' appears.

Use the errorMessages field in the options:

var options = {
    errorMessages: {
        MissingPasswordError: 'No password was given',
        AttemptTooSoonError: 'Account is currently locked. Try again later',
        TooManyAttemptsError: 'Account locked due to too many failed login attempts',
        NoSaltValueStoredError: 'Authentication not possible. No salt value stored',
        IncorrectPasswordError: 'Password or username are incorrect',
        IncorrectUsernameError: 'Password or username are incorrect',
        MissingUsernameError: 'No username was given',
        UserExistsError: 'A user with the given username is already registered'
    }
};

Account.plugin(passportLocalMongoose,options);

You can do it as follow:

Account.plugin(passportLocalMongoose, { usernameField: 'email', errorMessages : { UserExistsError : 'A user with the given email is already registered.' } });

you can change the options



    var options = {
            MissingPasswordError: 'No password was given',
            AttemptTooSoonError: 'Account is currently locked. Try again later',
            TooManyAttemptsError: 'Account locked due to too many failed login attempts',
            NoSaltValueStoredError: 'Authentication not possible. No salt value stored',
            IncorrectPasswordError: 'Password or username are incorrect',
            IncorrectUsernameError: 'Password or username are incorrect',
            MissingUsernameError: 'No username was given',
            UserExistsError: 'A user with the given username is already registered'
            };

        Account.plugin(passportLocalMongoose,options);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM