passport-local-mongoose Error message change

无人久伴 提交于 2021-01-28 04:34:03

问题


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.


回答1:


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);



回答2:


You can do it as follow:

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




回答3:


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);



来源:https://stackoverflow.com/questions/35856018/passport-local-mongoose-error-message-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!