|
1 | | -const errors = require('@feathersjs/errors'); |
2 | | -const makeDebug = require('debug'); |
3 | | -const comparePasswords = require('./helpers/compare-passwords'); |
4 | | -const deconstructId = require('./helpers/deconstruct-id'); |
5 | | -const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid'); |
6 | | -const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings'); |
7 | | -const getUserData = require('./helpers/get-user-data'); |
8 | | -const hashPassword = require('./helpers/hash-password'); |
9 | | -const notifier = require('./helpers/notifier'); |
10 | | - |
11 | | -const debug = makeDebug('authLocalMgnt:resetPassword'); |
12 | | - |
13 | | -module.exports = { |
14 | | - resetPwdWithLongToken, |
15 | | - resetPwdWithShortToken |
16 | | -}; |
17 | | - |
18 | | -async function resetPwdWithLongToken (options, resetToken, password, field) { |
19 | | - ensureValuesAreStrings(resetToken, password); |
20 | | - |
21 | | - const result = await resetPassword(options, { resetToken }, { resetToken }, password, field); |
22 | | - return result; |
23 | | -} |
24 | | - |
25 | | -async function resetPwdWithShortToken (options, resetShortToken, identifyUser, password, field) { |
26 | | - ensureValuesAreStrings(resetShortToken, password); |
27 | | - ensureObjPropsValid(identifyUser, options.identifyUserProps); |
28 | | - |
29 | | - const result = await resetPassword(options, identifyUser, { resetShortToken }, password, field); |
30 | | - return result; |
31 | | -} |
32 | | - |
33 | | -async function resetPassword (options, query, tokens, password, field) { |
34 | | - debug('resetPassword', query, tokens, password); |
35 | | - const usersService = options.app.service(options.service); |
36 | | - const usersServiceIdName = usersService.id; |
37 | | - const promises = []; |
38 | | - let users; |
39 | | - |
40 | | - if (tokens.resetToken) { |
41 | | - let id = deconstructId(tokens.resetToken); |
42 | | - users = await usersService.get(id); |
43 | | - } else if (tokens.resetShortToken) { |
44 | | - users = await usersService.find({ query }); |
45 | | - } else { |
46 | | - throw new errors.BadRequest('resetToken and resetShortToken are missing. (authLocalMgnt)', { |
47 | | - errors: { $className: 'missingToken' } |
48 | | - }); |
49 | | - } |
50 | | - |
51 | | - const checkProps = options.skipIsVerifiedCheck ? ['resetNotExpired'] : ['resetNotExpired', 'isVerified']; |
52 | | - const user1 = getUserData(users, checkProps); |
53 | | - |
54 | | - Object.keys(tokens).forEach(key => { |
55 | | - promises.push( |
56 | | - comparePasswords( |
57 | | - tokens[key], |
58 | | - user1[key], |
59 | | - () => |
60 | | - new errors.BadRequest('Reset Token is incorrect. (authLocalMgnt)', { |
61 | | - errors: { $className: 'incorrectToken' } |
62 | | - }) |
63 | | - ) |
64 | | - ); |
65 | | - }); |
66 | | - |
67 | | - try { |
68 | | - await Promise.all(promises); |
69 | | - } catch (err) { |
70 | | - await usersService.patch(user1[usersServiceIdName], { |
71 | | - resetToken: null, |
72 | | - resetShortToken: null, |
73 | | - resetExpires: null |
74 | | - }); |
75 | | - |
76 | | - throw new errors.BadRequest('Invalid token. Get for a new one. (authLocalMgnt)', { |
77 | | - errors: { $className: 'invalidToken' } |
78 | | - }); |
79 | | - } |
80 | | - |
81 | | - const user2 = await usersService.patch(user1[usersServiceIdName], { |
82 | | - password: await hashPassword(options.app, password, field), |
83 | | - resetToken: null, |
84 | | - resetShortToken: null, |
85 | | - resetExpires: null |
86 | | - }); |
87 | | - |
88 | | - const user3 = await notifier(options.notifier, 'resetPwd', user2); |
89 | | - return options.sanitizeUserForClient(user3); |
90 | | -} |
| 1 | +const errors = require('@feathersjs/errors'); |
| 2 | +const makeDebug = require('debug'); |
| 3 | +const comparePasswords = require('./helpers/compare-passwords'); |
| 4 | +const deconstructId = require('./helpers/deconstruct-id'); |
| 5 | +const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid'); |
| 6 | +const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings'); |
| 7 | +const getUserData = require('./helpers/get-user-data'); |
| 8 | +const hashPassword = require('./helpers/hash-password'); |
| 9 | +const notifier = require('./helpers/notifier'); |
| 10 | + |
| 11 | +const debug = makeDebug('authLocalMgnt:resetPassword'); |
| 12 | + |
| 13 | +module.exports = { |
| 14 | + resetPwdWithLongToken, |
| 15 | + resetPwdWithShortToken |
| 16 | +}; |
| 17 | + |
| 18 | +async function resetPwdWithLongToken (options, resetToken, password, field, notifierOptions = {}) { |
| 19 | + ensureValuesAreStrings(resetToken, password); |
| 20 | + |
| 21 | + const result = await resetPassword(options, { resetToken }, { resetToken }, password, field, notifierOptions); |
| 22 | + return result; |
| 23 | +} |
| 24 | + |
| 25 | +async function resetPwdWithShortToken (options, resetShortToken, identifyUser, password, field, notifierOptions = {}) { |
| 26 | + ensureValuesAreStrings(resetShortToken, password); |
| 27 | + ensureObjPropsValid(identifyUser, options.identifyUserProps); |
| 28 | + |
| 29 | + const result = await resetPassword(options, identifyUser, { resetShortToken }, password, field, notifierOptions); |
| 30 | + return result; |
| 31 | +} |
| 32 | + |
| 33 | +async function resetPassword (options, query, tokens, password, field, notifierOptions = {}) { |
| 34 | + debug('resetPassword', query, tokens, password); |
| 35 | + const usersService = options.app.service(options.service); |
| 36 | + const usersServiceIdName = usersService.id; |
| 37 | + const promises = []; |
| 38 | + let users; |
| 39 | + |
| 40 | + if (tokens.resetToken) { |
| 41 | + let id = deconstructId(tokens.resetToken); |
| 42 | + users = await usersService.get(id); |
| 43 | + } else if (tokens.resetShortToken) { |
| 44 | + users = await usersService.find({ query }); |
| 45 | + } else { |
| 46 | + throw new errors.BadRequest('resetToken and resetShortToken are missing. (authLocalMgnt)', { |
| 47 | + errors: { $className: 'missingToken' } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + const checkProps = options.skipIsVerifiedCheck ? ['resetNotExpired'] : ['resetNotExpired', 'isVerified']; |
| 52 | + const user1 = getUserData(users, checkProps); |
| 53 | + |
| 54 | + Object.keys(tokens).forEach(key => { |
| 55 | + promises.push( |
| 56 | + comparePasswords( |
| 57 | + tokens[key], |
| 58 | + user1[key], |
| 59 | + () => |
| 60 | + new errors.BadRequest('Reset Token is incorrect. (authLocalMgnt)', { |
| 61 | + errors: { $className: 'incorrectToken' } |
| 62 | + }) |
| 63 | + ) |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + try { |
| 68 | + await Promise.all(promises); |
| 69 | + } catch (err) { |
| 70 | + await usersService.patch(user1[usersServiceIdName], { |
| 71 | + resetToken: null, |
| 72 | + resetShortToken: null, |
| 73 | + resetExpires: null |
| 74 | + }); |
| 75 | + |
| 76 | + throw new errors.BadRequest('Invalid token. Get for a new one. (authLocalMgnt)', { |
| 77 | + errors: { $className: 'invalidToken' } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + const user2 = await usersService.patch(user1[usersServiceIdName], { |
| 82 | + password: await hashPassword(options.app, password, field), |
| 83 | + resetToken: null, |
| 84 | + resetShortToken: null, |
| 85 | + resetExpires: null |
| 86 | + }); |
| 87 | + |
| 88 | + const user3 = await notifier(options.notifier, 'resetPwd', user2, notifierOptions); |
| 89 | + return options.sanitizeUserForClient(user3); |
| 90 | +} |
0 commit comments