All files / src identity-change.js

100% Statements 23/23
100% Branches 0/0
100% Functions 2/2
100% Lines 23/23

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  1x 1x 1x 1x 1x 1x 1x 1x   1x   1x       16x 16x 16x   16x 16x   16x 16x   16x 16x   4x         12x             12x 12x    
 
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const getLongToken = require('./helpers/get-long-token');
const getShortToken = require('./helpers/get-short-token');
const getUserData = require('./helpers/get-user-data');
const notifier = require('./helpers/notifier');
 
const debug = makeDebug('authLocalMgnt:identityChange');
 
module.exports = identityChange;
 
async function identityChange (options, identifyUser, password, changesIdentifyUser) {
  // note this call does not update the authenticated user info in hooks.params.user.
  debug('identityChange', password, changesIdentifyUser);
  const usersService = options.app.service(options.service);
  const usersServiceIdName = usersService.id;
 
  ensureObjPropsValid(identifyUser, options.identifyUserProps);
  ensureObjPropsValid(changesIdentifyUser, options.identifyUserProps);
 
  const users = await usersService.find({ query: identifyUser });
  const user1 = getUserData(users);
 
  try {
    await comparePasswords(password, user1.password, () => {});
  } catch (err) {
    throw new errors.BadRequest('Password is incorrect.',
      { errors: { password: 'Password is incorrect.', $className: 'badParams' } }
    );
  }
 
  const user2 = await usersService.patch(user1[usersServiceIdName], {
    verifyExpires: Date.now() + options.delay,
    verifyToken: await getLongToken(options.longTokenLen),
    verifyShortToken: await getShortToken(options.shortTokenLen, options.shortTokenDigits),
    verifyChanges: changesIdentifyUser
  });
 
  const user3 = await notifier(options.notifier, 'identityChange', user2, null);
  return options.sanitizeUserForClient(user3);
}