【问题标题】:passport-local-mongoose Error message change护照本地猫鼬错误消息更改
【发布时间】:2016-06-21 17:28:20
【问题描述】:

我想用本地 mongoose 中间件更改我的错误消息。但它没有用:

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;

这就是我的 Account.js 和登录/注册工作完美

我的问题是,当我输入错误的用户名/密码时,会出现旧消息“用户名或密码不正确”。

【问题讨论】:

  • 您是如何收到错误消息的?

标签: javascript node.js express error-handling passport-local


【解决方案1】:

您可以更改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);

【讨论】:

    【解决方案2】:

    在选项中使用errorMessages 字段:

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

    【讨论】:

      【解决方案3】:

      你可以这样做:

      Account.plugin(passportLocalMongoose, { usernameField: 'email', errorMessages : { UserExistsError : '具有给定电子邮件的用户已经注册。' } });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-19
        • 2017-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-25
        • 1970-01-01
        • 2017-06-30
        相关资源
        最近更新 更多