All files / src verify-signup-set-password.js

100% Statements 32/32
83.33% Branches 5/6
100% Functions 5/5
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1031x 1x 1x 1x 1x 1x 1x   1x   1x                     28x   28x             16x                   40x 36x   32x                 20x       60x 60x 60x   60x 60x         40x 4x   4x           36x               36x 36x     40x   40x                 40x 40x      
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
const getUserData = require('./helpers/get-user-data');
const hashPassword = require('./helpers/hash-password');
const notifier = require('./helpers/notifier');
 
const debug = makeDebug('authLocalMgnt:verifySignupSetPassword');
 
module.exports = {
  verifySignupSetPasswordWithLongToken,
  verifySignupSetPasswordWithShortToken
};
 
async function verifySignupSetPasswordWithLongToken (
  options,
  verifyToken,
  password,
  field
) {
  ensureValuesAreStrings(verifyToken, password);
 
  const result = await verifySignupSetPassword(
    options,
    { verifyToken },
    { verifyToken },
    password,
    field
  );
  return result;
}
 
async function verifySignupSetPasswordWithShortToken (
  options,
  verifyShortToken,
  identifyUser,
  password,
  field
) {
  ensureValuesAreStrings(verifyShortToken, password);
  ensureObjPropsValid(identifyUser, options.identifyUserProps);
 
  const result = await verifySignupSetPassword(
    options,
    identifyUser,
    {
      verifyShortToken
    },
    password,
    field
  );
  return result;
}
 
async function verifySignupSetPassword (options, query, tokens, password, field) {
  debug('verifySignupSetPassword', query, tokens, password);
  const usersService = options.app.service(options.service);
  const usersServiceIdName = usersService.id;
 
  const users = await usersService.find({ query });
  const user1 = getUserData(users, [
    'isNotVerifiedOrHasVerifyChanges',
    'verifyNotExpired'
  ]);
 
  if (!Object.keys(tokens).every((key) => tokens[key] === user1[key])) {
    await eraseVerifyPropsSetPassword(user1, user1.isVerified, {}, password, field);
 
    throw new errors.BadRequest(
      'Invalid token. Get for a new one. (authLocalMgnt)',
      { errors: { $className: 'badParam' } }
    );
  }
 
  const user2 = await eraseVerifyPropsSetPassword(
    user1,
    user1.verifyExpires > Date.now(),
    user1.verifyChanges || {},
    password,
    field
  );
 
  const user3 = await notifier(options.notifier, 'verifySignupSetPassword', user2);
  return options.sanitizeUserForClient(user3);
 
  async function eraseVerifyPropsSetPassword (user, isVerified, verifyChanges, password, field) {
    const hashedPassword = await hashPassword(options.app, password, field);
 
    const patchToUser = Object.assign({}, verifyChanges || {}, {
      isVerified,
      verifyToken: null,
      verifyShortToken: null,
      verifyExpires: null,
      verifyChanges: {},
      password: hashedPassword
    });
 
    const result = await usersService.patch(user[usersServiceIdName], patchToUser, {});
    return result;
  }
}