Skip to content

Commit 64705f5

Browse files
authored
fix(authentication-local): Allow to hash passwords in array data (#1936)
1 parent d925c1b commit 64705f5

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

packages/authentication-local/src/hooks/hash-password.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ export default function hashPassword (field: string, options: HashPasswordOption
2424
}
2525

2626
const { app, data, params } = context;
27-
const password = get(data, field);
2827

29-
if (data === undefined || password === undefined) {
30-
debug(`hook.data or hook.data.${field} is undefined. Skipping hashPassword hook.`);
28+
if (data === undefined) {
29+
debug(`hook.data is undefined. Skipping hashPassword hook.`);
3130
return context;
3231
}
3332

@@ -44,9 +43,21 @@ export default function hashPassword (field: string, options: HashPasswordOption
4443
throw new BadRequest(`Could not find '${strategy}' strategy to hash password`);
4544
}
4645

47-
const hashedPassword: string = await localStrategy.hashPassword(password, params);
46+
const addHashedPassword = async (data: any) => {
47+
const password = get(data, field);
4848

49-
context.data = set(cloneDeep(data), field, hashedPassword);
49+
if (password === undefined) {
50+
debug(`hook.data.${field} is undefined, not hashing password`);
51+
return data;
52+
}
53+
54+
const hashedPassword: string = await localStrategy.hashPassword(password, params);
55+
56+
return set(cloneDeep(data), field, hashedPassword);
57+
}
58+
59+
context.data = Array.isArray(data) ? await Promise.all(data.map(addHashedPassword)) :
60+
await addHashedPassword(data);
5061

5162
return context;
5263
};

packages/authentication-local/test/fixture.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = (app = feathers()) => {
2525

2626
app.use('/authentication', authentication);
2727
app.use('/users', memory({
28+
multi: [ 'create' ],
2829
paginate: {
2930
default: 10,
3031
max: 20

packages/authentication-local/test/hooks/hash-password.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ describe('@feathersjs/authentication-local/hooks/hash-password', () => {
8989
assert.notStrictEqual(user.password, password);
9090
});
9191

92+
it('hashes password on array data', async () => {
93+
const password = 'supersecret';
94+
95+
const users = await app.service('users').create([{
96+
email: 'dave@hashpassword.com',
97+
password
98+
}, {
99+
email: 'dave2@hashpassword.com',
100+
password: 'secret2'
101+
}]);
102+
103+
assert.notStrictEqual(users[0].password, password);
104+
assert.notStrictEqual(users[1].password, 'secret2');
105+
});
106+
92107
it('does nothing when field is not present', async () => {
93108
const user = await app.service('users').create({
94109
email: 'dave@hashpassword.com'

0 commit comments

Comments
 (0)