【发布时间】:2014-08-21 12:47:46
【问题描述】:
我正在为注册和登录用户身份验证设置护照。现在我看到我可以通过回调验证信息,但是我也可以验证用户模型中的信息。例如,如果我想让电子邮件字段成为必填项,那么我可以执行以下操作:
护照验证
// Create the local sign up with Passport
passport.use('local-signup', new LocalStrategy({
usernameField : 'userEmail', // Email Field
passwordField : 'userPassword', // Password Field
passReqToCallback : true
},
function(req, email, password, done) {
// Find a user with this email
User.findOne({ "local.email": email }, function(err, user) {
if (err) throw err;
// If a user with this email already exists, continue with failureRedirect
if (user) {
return done(null);
} else {
// Otherwise create a new user with the email and generate a hashed password
User.create({
local: {
email: email
}
}, function(err, user) {
if (err) throw err;
user.local.password = user.generateHash(password);
// Return the user and finish! :)
return done(null, user);
});
}
});
};
}));
使用用户模型
var userSchema = new Schema({
local: {
email: {
type: String,
unique: true // Make it unique! Handles entire validation
},
password: {
type: String
},
}
});
推荐其中哪些以及为什么?
【问题讨论】:
标签: node.js validation mongoose passport.js