【问题标题】:Cannot pass more parameters in passport.js - MEAN Stack无法在passport.js中传递更多参数 - MEAN Stack
【发布时间】:2016-08-04 09:45:11
【问题描述】:

您好,我的 passport.js 中有以下代码:

passport.use('local-signup', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password',
    nameField: 'fullname',
    passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done, fullname) {

    // asynchronous
    // User.findOne wont fire unless data is sent back
    process.nextTick(function() {

    // find a user whose email is the same as the forms email
    // we are checking to see if the user trying to login already exists
    User.findOne({ 'local.email' :  email }, function(err, user) {
        // if there are any errors, return the error
        if (err)
            return done(err);

        // check to see if theres already a user with that email
        if (user) {
            return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
        } else {

            // if there is no user with that email
            // create the user
            var newUser            = new User();

            // set the user's local credentials
            newUser.local.email    = email;
            newUser.local.password = password;
            newUser.local.fullname = fullname;
            newUser.local.role = "default";

            // save the user
            newUser.save(function(err) {
                if (err) {
                    throw err;
                } 
                console.log(newUser);
                return done(null, newUser);

            });
        }

    });    

    });

}));

我需要将全名保存到数据库中,但遗憾的是没有添加,因为完成后是最后一个参数。但是如果在 done 之前放了全名,则找不到返回 done 并给我一个应用程序崩溃。

您认为有什么解决办法?

【问题讨论】:

  • 你不能从req 参数中获取全名(nameField)吗?
  • 不,我不能,我刚试过。

标签: javascript passport.js mean-stack mean


【解决方案1】:

您可以从 req 参数中检索全名。如果您使用的是bodyparser,那么它就像req.body.fullname 一样简单。在需要的地方使用它。您需要确保从表单中发送fullname 以及电子邮件和密码。

护照的本地策略不支持usernameFieldpasswordField 以外的其他输入。如果您需要用户的其他输入,则需要从原始请求中检索它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2014-11-05
    • 2019-06-10
    • 2016-04-14
    相关资源
    最近更新 更多