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

Commit

Permalink
Do not skip users that have no password (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Mar 25, 2018
1 parent 9bfbdef commit 1c71cad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
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

0 comments on commit 1c71cad

Please sign in to comment.