使用 MySQL 在 PassportJs 中对用户进行多序列化
Multiple Serialize User in PassportJs using MySQL
我正在使用 Nodejs MySQL..
开发应用程序
对于登录身份验证,我正在使用 passportJS。
此应用程序中有两个登录名,一个用于管理员,另一个用于客户。所以单独 tables 有用户和注册。
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 登录...它会检查用户 table 是否有相同的 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]);
});
}));
对于管理员,我使用电子邮件作为登录用户名
客户使用手机号登录
注册
用户
你不必序列化 只是 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
中的对象,您可以确定要查询的 table:
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]);
}
});
});
我正在使用 Nodejs MySQL..
开发应用程序对于登录身份验证,我正在使用 passportJS。 此应用程序中有两个登录名,一个用于管理员,另一个用于客户。所以单独 tables 有用户和注册。
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 登录...它会检查用户 table 是否有相同的 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]);
});
}));
对于管理员,我使用电子邮件作为登录用户名
客户使用手机号登录
注册
用户
你不必序列化 只是 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
中的对象,您可以确定要查询的 table:
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]);
}
});
});