-
-
Notifications
You must be signed in to change notification settings - Fork 795
Expand file tree
/
Copy pathhash-password.ts
More file actions
64 lines (48 loc) · 2.03 KB
/
hash-password.ts
File metadata and controls
64 lines (48 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import get from 'lodash/get';
import set from 'lodash/set';
import cloneDeep from 'lodash/cloneDeep';
import { BadRequest } from '@feathersjs/errors';
import Debug from 'debug';
import { HookContext } from '@feathersjs/feathers';
import { LocalStrategy } from '../strategy';
const debug = Debug('@feathersjs/authentication-local/hooks/hash-password');
export interface HashPasswordOptions {
authentication?: string;
strategy?: string;
}
export default function hashPassword (field: string, options: HashPasswordOptions = {}) {
if (!field) {
throw new Error('The hashPassword hook requires a field name option');
}
return async (context: HookContext<any, any>) => {
if (context.type !== 'before') {
throw new Error('The \'hashPassword\' hook should only be used as a \'before\' hook');
}
const { app, data, params } = context;
if (data === undefined) {
debug('hook.data is undefined. Skipping hashPassword hook.');
return context;
}
const authService = app.defaultAuthentication(options.authentication);
const { strategy = 'local' } = options;
if (!authService || typeof authService.getStrategies !== 'function') {
throw new BadRequest('Could not find an authentication service to hash password');
}
const [ localStrategy ] = authService.getStrategies(strategy) as LocalStrategy[];
if (!localStrategy || typeof localStrategy.hashPassword !== 'function') {
throw new BadRequest(`Could not find '${strategy}' strategy to hash password`);
}
const addHashedPassword = async (data: any) => {
const password = get(data, field);
if (password === undefined) {
debug(`hook.data.${field} is undefined, not hashing password`);
return data;
}
const hashedPassword: string = await localStrategy.hashPassword(password, params);
return set(cloneDeep(data), field, hashedPassword);
}
context.data = Array.isArray(data) ? await Promise.all(data.map(addHashedPassword)) :
await addHashedPassword(data);
return context;
};
}