Javascript 我可以从passport.com传回我自己的数据(对象)吗?使用(';local';)进行passport.authenticate?
我想将我自己的数据从passport.use传递到passport.authenticate 我认为info参数在Javascript 我可以从passport.com传回我自己的数据(对象)吗?使用(';local';)进行passport.authenticate?,javascript,node.js,passport.js,Javascript,Node.js,Passport.js,我想将我自己的数据从passport.use传递到passport.authenticate 我认为info参数在 passport.authenticate('local',函数(err,user,info) 可以用来做那件事 有没有办法做到这一点 我的授权路线 passport.authenticate('local-register', (err, user, info) => { if (err) { return next(err); // 5
passport.authenticate('local',函数(err,user,info)
可以用来做那件事
有没有办法做到这一点
我的授权路线
passport.authenticate('local-register', (err, user, info) => {
if (err) {
return next(err); // 500 status
}
console.log(info);
if (info) {
console.log('rendering info ' + info);
return res.render('auth/register', { info });
} else {
if (!user) {
return res.status(409).render('auth/register');
}
req.login(user, err => {
if (err) {
console.log(err);
return next(err);
}
return res.redirect('auth/profile');
});
}
})(req, res, next);
});
我的配置文件
module.exports = passport => {
passport.use(
'local-register',
new LocalStrategy(
{
...
},
(req, email, password, done) => {
// Check if form is filled out correctly
let errors = [];
//check for same email
SCUser.findOne({ 'local.email': req.body.email }, (err, local) => {
if (local) errors.push({ text: 'Email already in use.' });
//check for same passwords
...
//check for password length
...
//abort if errors are found
if (errors.length > 0) {
const info = {
errors: errors,
...,
};
console.log(`returning info ${info}`);
return done(null, info);
}
//form is filled in correctly create a user
else {
...
}
...
到目前为止我尝试过的随机事件:
在info
后面添加,form\u validate
&将所需变量更改为form\u validate
,不会将其传递到auth文件
可能有更好的方法来处理表单验证,我还没有找到,如果您有任何建议,请告诉我,但我还是想知道是否可以通过passports方法传递自定义对象。查看文档中的示例:
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
如果您确定用户名/密码不正确,可以调用done
,最后一个参数是您想要的任何对象
return done(null, false, { message: "Incorrect password", otherData: "my other data"});
如果成功,您可以传递用户,但没有理由不能向用户添加更多数据,或者传递完全不同的内容
return done(null, {username: "user123", otherData: myData, customString: "myString"});
根据文档,您可以在成功时传递用户对象,在失败时传递信息对象。您是否确实尝试过查看?首先进行实验,如果您有问题,然后将其发布到SO。@Pop-A-Stash``将传递一个可选的信息参数,其中包含由策略的验证回调提供的其他详细信息。``信息不会传递到failure但是在验证回调上?你到底想做什么?我看到提到了“表单验证”。Passport与该任务完全无关;它只用于验证用户。谢谢你的帮助:)