问题
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