本文介绍了护照当地猫鼬:未给用户名提供错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的前端登录表单上,我只有输入字段emailpassword. Passport local需要一个usernameField名称,并抛出一个[MissingUsernameError: No username was given].有什么方法可以让本地护照不期望usernameField?

On my frontend login form, I only have input fields email and password. Passport local expects a usernameField name and throws a [MissingUsernameError: No username was given]. Is there any way to let passport local not expect a usernameField?

推荐答案

如果要使用电子邮件地址而不是用户名,则需要告诉护照.为此,请参见以下代码片段:

If you want to use email address instead of username, then you need to tell passport that. To do that, kindly see the following code snippet:

passport.use(new LocalStrategy({
            usernameField: 'email', // this is where you do that
            passwordField: 'password'
        },
       (email, password, done) => {
            User.findOne({
                email: email
            }, (error, user) => {
                if (error) {
                    return done(error);
                }
                if (!user) {
                    return done(null, false, {
                        message: 'Username or password incorrect'
                    });
                }


                // Do other validation/check if any

                return done(null, user);
            });
        }
    ));

这篇关于护照当地猫鼬:未给用户名提供错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:22