-->

更新或passport.js本地策略添加字段?(Update or add fields in pa

2019-10-19 16:18发布

IM遍布文档,但我似乎无法找到一个方法来更新凭据。

这是我能够通过分析代码回暖。

passport.deserializeUser(function(id, done) {
    AppUser.findById(id, function(err, user) {
        done(err, user);
    });
});

DeserializeUser似乎是有用的,但我不知道如何使用它来更新或添加字段?

我试图破解路程,从登录复制的逻辑和它的意义。

passport.use('local-update', new LocalStrategy({
    usernameField : 'username',
    passReqToCallback : true
},
function(req, username, done) {

    console.log(req)
    // asynchronous
    // AppUser.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
    //     AppUser.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.'));
    //             return done(null, false);
    //         } else {

    //             // if there is a user with that email
    //             // create the username
    //             var updateUser = new AppUser();

    //             // set the user's local credentials
    //             newUser.local.email    = email;
    //             newUser.local.password = newUser.generateHash(password);

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

    //     });    

    // });

}));

然后在表格上提交我这样做。

app.post('/profile', passport.authenticate('local-update', {
    successRedirect : '/', // redirect to the secure profile section
    failureRedirect : '/signup' // redirect back to the signup page if there is an error
    //failureFlash : true // allow flash messages
}));

这将导致失败的重定向。

这是行不通的,因为没有任何回应,但我需要找到MongoDB中的模型。 我想在控制台看到第一REQ,这样或许我可以看到如何找到模型,但并没有什么显示出来。

上面很明显的hackish代码,但是这是我能做到的最好。 我需要一个具体的答案,我相信它的简单,我很想念它的文档!

编辑:这里的想法是,当用户注册/登录时他们提供了一个电子邮件。 一旦用户登录和帐号创建他们可以创建一个用户名。

编辑:所以我无法弄清楚如何使护照更新请求,但在我的路由器我有这样的事情。

app.post('/', function(req, res) {
    if (req.user) {
        AppUser.findOne({ _id: req.user.id }, function (err, user) {
            user.local.username  = req.body.username;
            user.save(function(err) {
                if (err){
                    console.log('Error')
                } else {
                    console.log('Sucess')
                }
            });
        });
    }
});

唯一的问题是浏览器的默认操作,它提交表单,并保持在环形重新加载页面。 但它确实更新我的MongoDB的模型。 我不得不向插件架构,而不得不添加属性在我的护照注册逻辑。

但是,我可以只添加这种逻辑到我的客户端代码,并抛出POST方法为骨干,并应努力!

Answer 1:

在这样的情况下,您可以添加一个callback array作为快速通道的参数。

我想你可以改变你的验证处理程序是这样的:

function(req, username, done) {
    //perform here only the validations you need to let the login pass
    if (validationSuccess) {
        done();
    } else {
    done('an error occured');
    }
}

因此,假设该功能将成功验证用户凭据,你可以写另一个问题:

function doSomethingAfter(req, res, next) {
    //do anything else you need here
    //e.g.
    SomeModel.create(req.body.username);
    //the response is available here
    res.send('Everything ok');
}

最后,您可以编辑您的路由功能

app.post('/profile', [passport.authenticate('local-update', {
    failureRedirect : '/signup' // redirect back to the signup page if there is an error
}), doSomethingAfter]);

这样一来,就可以进行认证,让你想 ,要求正确地验证什么。 如果您需要添加更多的功能,你必须将它们添加到阵列和调用next()每一个。

希望能帮助到你。



文章来源: Update or add fields in passport.js local-strategy?