【发布时间】:2015-06-03 02:41:35
【问题描述】:
我想向我的 Redis 服务器查询用户名的匹配密码。我怎样才能做到这一点?我对 Redis 和 Node 的经验都很少,所以我找不到存储这些的密钥。
非常感谢任何帮助!
【问题讨论】:
标签: node.js redis node-redis nodebb
我想向我的 Redis 服务器查询用户名的匹配密码。我怎样才能做到这一点?我对 Redis 和 Node 的经验都很少,所以我找不到存储这些的密钥。
非常感谢任何帮助!
【问题讨论】:
标签: node.js redis node-redis nodebb
查看文件 /src/routes/authentication.js。那里有 Auth.login 函数,它获取用户名、密码作为参数。然后你在用户对象上有 getUidByUserslug 函数,它首先被调用并从名为'userslug:uid'的redis哈希返回你的用户ID(_uid)(查看/src/user .js 文件 db.getObjectField('userslug:uid', userslug, callback); function)。下一步是通过用户 ID 从 'user:' + uid 哈希中获取用户,存储在 redis 中。这是使用 db.getObjectFields('user:' + uid, ['password', 'banned'], next); authenticate.js 文件中的函数。
以下是Auth.login功能:
Auth.login = function(req, username, password, next) {
if (!username || !password) {
return next(new Error('[[error:invalid-password]]'));
}
var userslug = utils.slugify(username);
var uid;
async.waterfall([
function(next) {
user.getUidByUserslug(userslug, next);
},
function(_uid, next) {
if (!_uid) {
return next(new Error('[[error:no-user]]'));
}
uid = _uid;
user.auth.logAttempt(uid, req.ip, next);
},
function(next) {
db.getObjectFields('user:' + uid, ['password', 'banned'], next);
},
function(userData, next) {
if (!userData || !userData.password) {
return next(new Error('[[error:invalid-user-data]]'));
}
if (userData.banned && parseInt(userData.banned, 10) === 1) {
return next(new Error('[[error:user-banned]]'));
}
Password.compare(password, userData.password, next);
},
function(passwordMatch, next) {
if (!passwordMatch) {
return next(new Error('[[error:invalid-password]]'));
}
user.auth.clearLoginAttempts(uid);
next(null, {uid: uid}, '[[success:authentication-successful]]');
}
], next);
};
【讨论】: