Passport / done 函数和 passReqToCallBack 可以一起使用吗?
Passport / done function and passReqToCallBack can be use together?
您好,我正在使用 passport-local,我想用它制定一个注册策略。
我希望用户提供用户名-密码-名字-姓氏进行注册,所以我尝试在我的策略中使用 passReqToCallback: true
passport.use('local-signup', new LocalStrategy({
passReqToCallBack: true
},
(req, username, password, done) => {
process.nextTick(() => {
User.findOne({ 'local.username' : username }, (err, user) => {
if (err) return done(err);
if (user) return done(null, false, { status: false, details: 'not ok' });
const newUser = new User();
newUser.local.username = username;
newUser.local.password = newUser.generateHash(password);
newUser.save((err) => {
if (err) throw err;
return done(null, newUser);
});
})
});
}))
然后使用 req.body.firstname... 以便将其保存在我的 bd 中。
这是问题所在:我无法使用 passReqToCallback 和 done 函数...
如果你能帮我解决这个问题,因为我读到的一切都告诉我,或者设置 passReqtoCallBack: true 如果我想使用 req 或者将它设置为 false 如果我已经完成不是函数错误....
谢谢
您正在尝试将用户名和密码传递给回调函数,但您尚未在新的 LocalStrategy 中定义用户名和密码字段,即:
passport.use('local.signup',new LocalStrategy({
usernameField: 'email', **//the usernameField contains the email or the name of the user**
passwordField: 'password',
passReqToCallback : true **//this line states pass request to callback is true**
}, function(req , email , password , done){ //this is callback function and it takes the req as its first argument then email , password and done as its respective arguments.
req.checkBody('email' , 'Invalid Email').notEmpty().isEmail();
req.checkBody('password' , 'Invalid password').notEmpty().isLength({min:4});
var errors = req.validationErrors();
if (errors) {
var messages = [];
errors.forEach(function(error){
messages.push(error.msg);
});
return done(null , false , req.flash('error' , messages));
}
User.findOne({email: email}, function(err , user){
if (err) {
return done(err);
}
if (user) {
return done(null , false , {message: "Email is all ready in use"});
}
if (!user) {
return done(null, false, { message: 'Incorrect email' });
}
var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.encryptPassword(password);
newUser.save(function(err){
if (err)
throw err;
return done(null, newUser);
});
});
}));
希望对您有所帮助。
您好,我正在使用 passport-local,我想用它制定一个注册策略。
我希望用户提供用户名-密码-名字-姓氏进行注册,所以我尝试在我的策略中使用 passReqToCallback: true
passport.use('local-signup', new LocalStrategy({
passReqToCallBack: true
},
(req, username, password, done) => {
process.nextTick(() => {
User.findOne({ 'local.username' : username }, (err, user) => {
if (err) return done(err);
if (user) return done(null, false, { status: false, details: 'not ok' });
const newUser = new User();
newUser.local.username = username;
newUser.local.password = newUser.generateHash(password);
newUser.save((err) => {
if (err) throw err;
return done(null, newUser);
});
})
});
}))
然后使用 req.body.firstname... 以便将其保存在我的 bd 中。
这是问题所在:我无法使用 passReqToCallback 和 done 函数...
如果你能帮我解决这个问题,因为我读到的一切都告诉我,或者设置 passReqtoCallBack: true 如果我想使用 req 或者将它设置为 false 如果我已经完成不是函数错误....
谢谢
您正在尝试将用户名和密码传递给回调函数,但您尚未在新的 LocalStrategy 中定义用户名和密码字段,即:
passport.use('local.signup',new LocalStrategy({
usernameField: 'email', **//the usernameField contains the email or the name of the user**
passwordField: 'password',
passReqToCallback : true **//this line states pass request to callback is true**
}, function(req , email , password , done){ //this is callback function and it takes the req as its first argument then email , password and done as its respective arguments.
req.checkBody('email' , 'Invalid Email').notEmpty().isEmail();
req.checkBody('password' , 'Invalid password').notEmpty().isLength({min:4});
var errors = req.validationErrors();
if (errors) {
var messages = [];
errors.forEach(function(error){
messages.push(error.msg);
});
return done(null , false , req.flash('error' , messages));
}
User.findOne({email: email}, function(err , user){
if (err) {
return done(err);
}
if (user) {
return done(null , false , {message: "Email is all ready in use"});
}
if (!user) {
return done(null, false, { message: 'Incorrect email' });
}
var newUser = new User();
newUser.local.email = email;
newUser.local.password = newUser.encryptPassword(password);
newUser.save(function(err){
if (err)
throw err;
return done(null, newUser);
});
});
}));
希望对您有所帮助。