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

Commit 1c71cad

Browse files
authored
Do not skip users that have no password (#60)
1 parent 9bfbdef commit 1c71cad

2 files changed

Lines changed: 28 additions & 33 deletions

File tree

lib/hooks/hash-password.js

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,23 @@ module.exports = function hashPassword (options = {}) {
3333
return Promise.resolve(context);
3434
}
3535

36-
let data;
36+
const dataIsArray = Array.isArray(context.data);
37+
const data = dataIsArray ? context.data : [ context.data ];
3738

38-
// make sure we actually have password fields
39-
if (Array.isArray(context.data)) {
40-
data = context.data.filter(item => {
41-
return item.hasOwnProperty(field);
42-
});
43-
} else if (context.data[field]) {
44-
data = context.data;
45-
}
46-
47-
// If the data doesn't have a password field
48-
// then don't attempt to hash it.
49-
if (data === undefined || (Array.isArray(data) && !data.length)) {
50-
debug(`'${field}' field is missing. Skipping hashPassword hook.`);
51-
return Promise.resolve(context);
52-
}
53-
54-
if (Array.isArray(data)) {
55-
debug(`Hashing passwords.`);
56-
57-
return Promise.all(data.map(item => {
39+
return Promise.all(data.map(item => {
40+
if (item.hasOwnProperty(field)) {
5841
return hashPw(item[field]).then(hashedPassword => {
59-
item[field] = hashedPassword;
60-
return item;
42+
return Object.assign(item, {
43+
[field]: hashedPassword
44+
});
6145
});
62-
}))
63-
.then(results => {
64-
context.data = results;
65-
return Promise.resolve(context);
66-
});
67-
}
46+
}
6847

69-
debug(`Hashing password.`);
70-
return hashPw(data[field]).then(hashedPassword => {
71-
context.data[field] = hashedPassword;
72-
return Promise.resolve(context);
48+
return item;
49+
})).then(results => {
50+
context.data = dataIsArray ? results : results[0];
51+
52+
return context;
7353
});
7454
};
7555
};

test/hooks/hash-password.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ describe('hooks:hashPassword', () => {
104104
});
105105
});
106106

107+
it('does not remove things if there is no password', () => {
108+
hook.data = [
109+
{ id: 0, password: 'secret' },
110+
{ id: 1 }
111+
];
112+
113+
return hashPassword()(hook).then(hook => {
114+
const { data } = hook;
115+
116+
expect(data.length).to.equal(2);
117+
expect(data[0].password).to.not.equal('secret');
118+
expect(data[1]).to.exist;
119+
});
120+
});
121+
107122
it('hashes with custom options', () => {
108123
hook.data = [
109124
{pass: 'secret'},

0 commit comments

Comments
 (0)