gpt4 book ai didi

mysql - 使用 MySQL 在 PassportJs 中多个序列化用户

转载 作者:搜寻专家 更新时间:2023-11-01 00:40:07 24 4
gpt4 key购买 nike

正在使用 Nodejs 和 MySQL 开发应用程序..

对于登录身份验证,我使用的是 passportJS。此应用程序中有两个登录名,一个用于管理员,另一个用于客户。所以单独的表是有 USERS 和 REGISTERS。

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]);
}
});

});

在反序列化用户中,如果我使用客户 ID 登录...它会检查用户表是否有相同的 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]);

});

}));

对于管理员,我使用电子邮件作为登录用户名

客户使用手机号登录

注册 registertable

用户 user table

最佳答案

你不必序列化只是 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 文档包含反射(reflect)用户是否为管理员的内容

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]);
}
});
});

关于mysql - 使用 MySQL 在 PassportJs 中多个序列化用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37832580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com