diff --git a/src/actions/ban.js b/src/actions/ban.js index 4bb983775..0d8349b20 100644 --- a/src/actions/ban.js +++ b/src/actions/ban.js @@ -50,7 +50,7 @@ function unlockUser({ username }) { module.exports = function banUser(opts) { return Promise .bind(this, opts.username) - .tap(userExists) - .return(opts) + .then(userExists) + .then(username => ({ ...opts, username })) .then(opts.ban ? lockUser : unlockUser); }; diff --git a/src/actions/getInternalData.js b/src/actions/getInternalData.js index 21dc745a4..165028883 100644 --- a/src/actions/getInternalData.js +++ b/src/actions/getInternalData.js @@ -1,5 +1,8 @@ +const Promise = require('bluebird'); const getInternalData = require('../utils/getInternalData.js'); module.exports = function internalData(message) { - return getInternalData.call(this, message.username); + return Promise + .bind(this, message.username) + .then(getInternalData); }; diff --git a/src/actions/getMetadata.js b/src/actions/getMetadata.js index dd3929d15..2ed2d41e3 100644 --- a/src/actions/getMetadata.js +++ b/src/actions/getMetadata.js @@ -3,10 +3,11 @@ const getMetadata = require('../utils/getMetadata.js'); const userExists = require('../utils/userExists.js'); module.exports = function getMetadataAction(message) { - const { username, audience } = message; + const { audience } = message; return Promise - .bind(this, username) + .bind(this, message.username) .then(userExists) - .then(() => getMetadata.call(this, username, audience)); + .then(username => [username, audience]) + .spread(getMetadata); }; diff --git a/src/actions/updateMetadata.js b/src/actions/updateMetadata.js index 7ce1598fa..60ce3756f 100644 --- a/src/actions/updateMetadata.js +++ b/src/actions/updateMetadata.js @@ -3,11 +3,9 @@ const updateMetadata = require('../utils/updateMetadata.js'); const userExists = require('../utils/userExists.js'); module.exports = function updateMetadataAction(message) { - const { username } = message; - return Promise - .bind(this, username) + .bind(this, message.username) .then(userExists) - .return(message) + .then(username => ({ ...message, username })) .then(updateMetadata); }; diff --git a/src/actions/updatePassword.js b/src/actions/updatePassword.js index 094c087c3..96e8a985e 100644 --- a/src/actions/updatePassword.js +++ b/src/actions/updatePassword.js @@ -37,16 +37,21 @@ function usernamePasswordReset(username, password) { * @param {String} username * @param {String} password */ -function setPassword(username, password) { +function setPassword(_username, password) { const { redis } = this; return Promise - .bind(this, username) + .bind(this, _username) .then(userExists) - .return(password) - .then(scrypt.hash) - .then(hash => redis.hset(redisKey(username, USERS_DATA), 'password', hash)) - .return(username); + .then(username => Promise.props({ + username, + hash: scrypt.hash(password), + })) + .then(({ username, hash }) => + redis + .hset(redisKey(username, USERS_DATA), 'password', hash) + .return(username) + ); } module.exports = exports = function updatePassword(opts) { @@ -72,7 +77,7 @@ module.exports = exports = function updatePassword(opts) { if (remoteip) { promise = promise.tap(function resetLock(username) { - return redis.del(redisKey(username, USERS_DATA, remoteip)); + return redis.del(redisKey(username, 'ip', remoteip)); }); } diff --git a/src/utils/userExists.js b/src/utils/userExists.js index ae69a626c..be39d8c12 100644 --- a/src/utils/userExists.js +++ b/src/utils/userExists.js @@ -1,13 +1,20 @@ const Errors = require('common-errors'); const redisKey = require('../utils/key.js'); -const { USERS_DATA } = require('../constants.js'); +const { USERS_DATA, USERS_ALIAS_TO_LOGIN } = require('../constants.js'); module.exports = function userExists(username) { return this .redis + .pipeline() + .hget(USERS_ALIAS_TO_LOGIN, username) .exists(redisKey(username, USERS_DATA)) - .then(exists => { - if (!exists) { + .exec() + .spread((alias, exists) => { + if (alias[1]) { + return alias[1]; + } + + if (!exists[1]) { throw new Errors.HttpStatusError(404, `"${username}" does not exists`); }