我正在使用带有mysql的nodejs开发应用程序。
使用passportjs进行登录身份验证。
此应用程序中有两个登录名,一个用于管理员,另一个用于客户。所以单独的表有用户和寄存器。
passport.serializeUser(function(user, done) {
done(null, {
id : user.id,
isAdmin : user.isAdmin // or some other property/check
});
});
// used to deserialize the user
passport.deserializeUser(function(user, done) {
var table = user.isAdmin ? 'register' : 'users';
connection.query('select * from ?? where id = ?', [ table, user.id ], function(err, rows) {
if (err) {
return done(err);
} else if (! Array.isArray(rows) || ! rows.length) {
return done();
} else {
return done(null, rows[0]);
}
});
});
在反序列化用户中,如果我使用customer id登录…它将使用user表检查相同的id…所以获取的数据是错误的
问题更新:
管理员的本地登录
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
connection.query("select * from users WHERE email = '" + email + "'",function(err,rows){
if (err)
return done(err);
if (!rows.length) {
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong
if (!( rows[0].password == password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, rows[0]);
});
}));
客户登录
passport.use('customer-login', new LocalStrategy({
usernameField : 'mobile',
passwordField : 'otp',
passReqToCallback : true
},
function(req, mobile, otp, done) {
connection.query("select * from register WHERE mobile = '" + mobile + "'",function(err,rows){
if (err)
return done(err);
if (!rows.length) {
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong
if (!( rows[0].otp == otp))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
console.log(rows);
return done(null, rows[0]);
});
}));
对于管理员AM,使用电子邮件作为登录用户名
客户AM使用手机号码登录
登记
用户
最佳答案
您不必只序列化serializeUser
中的用户ID,它也可以是包含(例如)管理状态的对象(从您的数据库内容来看,cust_code
似乎只存在于普通用户,因此我们可以使用它来检查用户是否是管理员):
passport.serializeUser(function(user, done) {
done(null, {
id : user.id,
isAdmin : user.cust_code === undefined // this does require that `cust_code`
// is defined for all regular users.
});
});
这显然假设
user
文档包含反映用户是否是管理员的内容。使用
deserializeUser
中的对象可以确定要查询的表:passport.deserializeUser(function(user, done) {
var table = user.isAdmin ? 'users' : 'register';
connection.query('select * from ?? where id = ?', [ table, user.id ], function(err, rows) {
if (err) {
return done(err);
} else if (! Array.isArray(rows) || ! rows.length) {
return done();
} else {
return done(null, rows[0]);
}
});
});