Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Do not skip users that have no password #60

Merged
merged 1 commit into from
Mar 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 13 additions & 33 deletions lib/hooks/hash-password.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,23 @@ module.exports = function hashPassword (options = {}) {
return Promise.resolve(context);
}

let data;
const dataIsArray = Array.isArray(context.data);
const data = dataIsArray ? context.data : [ context.data ];

// make sure we actually have password fields
if (Array.isArray(context.data)) {
data = context.data.filter(item => {
return item.hasOwnProperty(field);
});
} else if (context.data[field]) {
data = context.data;
}

// If the data doesn't have a password field
// then don't attempt to hash it.
if (data === undefined || (Array.isArray(data) && !data.length)) {
debug(`'${field}' field is missing. Skipping hashPassword hook.`);
return Promise.resolve(context);
}

if (Array.isArray(data)) {
debug(`Hashing passwords.`);

return Promise.all(data.map(item => {
return Promise.all(data.map(item => {
if (item.hasOwnProperty(field)) {
return hashPw(item[field]).then(hashedPassword => {
item[field] = hashedPassword;
return item;
return Object.assign(item, {
[field]: hashedPassword
});
});
}))
.then(results => {
context.data = results;
return Promise.resolve(context);
});
}
}

debug(`Hashing password.`);
return hashPw(data[field]).then(hashedPassword => {
context.data[field] = hashedPassword;
return Promise.resolve(context);
return item;
})).then(results => {
context.data = dataIsArray ? results : results[0];

return context;
});
};
};
15 changes: 15 additions & 0 deletions test/hooks/hash-password.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ describe('hooks:hashPassword', () => {
});
});

it('does not remove things if there is no password', () => {
hook.data = [
{ id: 0, password: 'secret' },
{ id: 1 }
];

return hashPassword()(hook).then(hook => {
const { data } = hook;

expect(data.length).to.equal(2);
expect(data[0].password).to.not.equal('secret');
expect(data[1]).to.exist;
});
});

it('hashes with custom options', () => {
hook.data = [
{pass: 'secret'},
Expand Down