All files / src/methods verify-signup-set-password.ts

100% Statements 37/37
72.73% Branches 8/11
100% Functions 6/6
100% Lines 36/36

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 1361x 1x 1x 1x 1x 1x 1x 1x                       1x   1x           28x   28x             16x     1x             44x 44x   36x                 20x                   64x 64x 64x   64x 64x         40x 4x   4x           36x             36x 36x             4x               4x 4x                 36x   36x                 36x 36x      
import { BadRequest } from '@feathersjs/errors';
import makeDebug from 'debug';
import ensureObjPropsValid from '../helpers/ensure-obj-props-valid';
import ensureValuesAreStrings from '../helpers/ensure-values-are-strings';
import getUserData from '../helpers/get-user-data';
import hashPassword from '../helpers/hash-password';
import isDateAfterNow from '../helpers/is-date-after-now';
import notifier from '../helpers/notifier';
 
import type {
  IdentifyUser,
  SanitizedUser,
  Tokens,
  User,
  VerifyChanges,
  VerifySignupSetPasswordOptions,
  VerifySignupSetPasswordWithShortTokenOptions
} from '../types';
 
const debug = makeDebug('authLocalMgnt:verifySignupSetPassword');
 
export async function verifySignupSetPasswordWithLongToken (
  options: VerifySignupSetPasswordOptions,
  verifyToken: string,
  password: string,
  notifierOptions = {}
): Promise<SanitizedUser> {
  ensureValuesAreStrings(verifyToken, password);
 
  const result = await verifySignupSetPassword(
    options,
    { verifyToken },
    { verifyToken },
    password,
    notifierOptions
  );
  return result;
}
 
export async function verifySignupSetPasswordWithShortToken (
  options: VerifySignupSetPasswordWithShortTokenOptions,
  verifyShortToken: string,
  identifyUser: IdentifyUser,
  password: string,
  notifierOptions = {}
): Promise<SanitizedUser> {
  ensureValuesAreStrings(verifyShortToken, password);
  ensureObjPropsValid(identifyUser, options.identifyUserProps);
 
  const result = await verifySignupSetPassword(
    options,
    identifyUser,
    {
      verifyShortToken
    },
    password,
    notifierOptions
  );
  return result;
}
 
async function verifySignupSetPassword (
  options: VerifySignupSetPasswordOptions,
  query: IdentifyUser,
  tokens: Tokens,
  password: string,
  notifierOptions = {}
): Promise<SanitizedUser> {
  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 eraseVerifyProps(user1, user1.isVerified, {});
 
    throw new BadRequest(
      'Invalid token. Get for a new one. (authLocalMgnt)',
      { errors: { $className: 'badParam' } }
    );
  }
 
  const user2 = await eraseVerifyPropsSetPassword(
    user1,
    isDateAfterNow(user1.verifyExpires),
    user1.verifyChanges || {},
    password
  );
 
  const user3 = await notifier(options.notifier, 'verifySignupSetPassword', user2, notifierOptions);
  return options.sanitizeUserForClient(user3);
 
  async function eraseVerifyProps (
    user: User,
    isVerified: boolean,
    verifyChanges: VerifyChanges
  ): Promise<User> {
    const patchToUser = Object.assign({}, verifyChanges || {}, {
      isVerified,
      verifyToken: null,
      verifyShortToken: null,
      verifyExpires: null,
      verifyChanges: {}
    });
 
    const result = await usersService.patch(user[usersServiceIdName], patchToUser, {});
    return result;
  }
 
  async function eraseVerifyPropsSetPassword (
    user: User,
    isVerified: boolean,
    verifyChanges: VerifyChanges,
    password: string
  ): Promise<User> {
    const hashedPassword = await hashPassword(options.app, password, options.passwordField);
 
    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;
  }
}