-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassword-change.ts
76 lines (64 loc) · 1.9 KB
/
password-change.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
import { BadRequest } from '@feathersjs/errors';
import makeDebug from 'debug';
import {
comparePasswords,
ensureObjPropsValid,
ensureValuesAreStrings,
getUserData,
hashPassword,
notify
} from '../helpers';
import type { Id, Params } from '@feathersjs/feathers';
import type {
IdentifyUser,
PasswordChangeOptions,
SanitizedUser,
NotifierOptions,
User
} from '../types';
const debug = makeDebug('authLocalMgnt:passwordChange');
export default async function passwordChange (
options: PasswordChangeOptions,
identifyUser: IdentifyUser,
oldPassword: string,
password: string,
notifierOptions: NotifierOptions = {},
params?: Params
): Promise<SanitizedUser> {
debug('passwordChange', oldPassword, password);
if (params && "query" in params) {
params = Object.assign({}, params);
delete params.query;
}
const {
app,
identifyUserProps,
passwordField,
skipPasswordHash,
sanitizeUserForClient,
service,
notifier
} = options;
const usersService = app.service(service);
const usersServiceId = usersService.id;
ensureValuesAreStrings(oldPassword, password);
ensureObjPropsValid(identifyUser, identifyUserProps);
const users = await usersService.find({
...params,
query: { ...identifyUser, $limit: 2 },
paginate: false,
}) as User[];
const user = getUserData(users);
try {
await comparePasswords(oldPassword, user.password);
} catch (err) {
throw new BadRequest('Current password is incorrect.', {
errors: { oldPassword: 'Current password is incorrect.' }
});
}
const patchedUser = await usersService.patch(user[usersServiceId] as Id, {
password: skipPasswordHash ? password : await hashPassword(app, password, passwordField)
}, Object.assign({}, params)) as User;
const userResult = await notify(notifier, 'passwordChange', patchedUser, notifierOptions);
return sanitizeUserForClient(userResult);
}