我目前在我的Node和Express项目中使用Passport登录并运行。现在我有一个用户集合,可以获取用户名和密码并正常登录。但是,我有2个不同的用户类型(护士和医生),除了大约4个不同的字段外,它们都有相似的字段。我不确定如何通过登录实现2个不同的用户。是否最好只让所有用户收集不同的字段,或者我是否应该为这两个用户分别收集?如果是这样我将如何实现呢?
我不确定发布的代码除了我的护照登录代码之外还有什么帮助
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
var bCrypt = require('bcrypt-nodejs');
module.exports = function(passport){
passport.use('login', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
// check in mongo if a user with username exists or not
User.findOne({ 'username' : username },
function(err, user) {
// In case of any error, return using the done method
if (err)
return done(err);
// Username does not exist, log the error and redirect back
if (!user){
console.log('User Not Found with username '+username);
return done(null, false, req.flash('message', 'User Not found.'));
}
// User exists but wrong password, log the error
if (!isValidPassword(user, password)){
console.log('Invalid Password');
return done(null, false, req.flash('message', 'Invalid Password')); // redirect back to login page
}
// User and password both match, return user from done method
// which will be treated like success
return done(null, user);
}
);
})
);
var isValidPassword = function(user, password){
return bCrypt.compareSync(password, user.password);
}
}