All files / src/methods verify-signup.ts

100% Statements 32/32
90.91% Branches 10/11
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 851x 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     40x               40x 40x      
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 notifier from '../helpers/notifier';
import isDateAfterNow from '../helpers/is-date-after-now';
 
import type { Query } from '@feathersjs/feathers';
 
import type {
  SanitizedUser,
  VerifySignupOptions,
  VerifySignupWithShortTokenOptions,
  Tokens,
  User,
  VerifyChanges,
  IdentifyUser
} from '../types';
 
const debug = makeDebug('authLocalMgnt:verifySignup');
 
export async function verifySignupWithLongToken (
  options: VerifySignupOptions,
  verifyToken: string,
  notifierOptions = {}
): Promise<SanitizedUser> {
  ensureValuesAreStrings(verifyToken);
 
  const result = await verifySignup(options, { verifyToken }, { verifyToken }, notifierOptions);
  return result;
}
 
export async function verifySignupWithShortToken (
  options: VerifySignupWithShortTokenOptions,
  verifyShortToken: string,
  identifyUser: IdentifyUser,
  notifierOptions = {}
): Promise<SanitizedUser> {
  ensureValuesAreStrings(verifyShortToken);
  ensureObjPropsValid(identifyUser, options.identifyUserProps);
 
  const result = await verifySignup(options, identifyUser, { verifyShortToken }, notifierOptions);
  return result;
}
 
async function verifySignup (
  options: VerifySignupOptions,
  query: Query,
  tokens: Tokens,
  notifierOptions = {}
): Promise<SanitizedUser> {
  debug('verifySignup', query, tokens);
  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 eraseVerifyProps(user1, isDateAfterNow(user1.verifyExpires), user1.verifyChanges || {});
  const user3 = await notifier(options.notifier, 'verifySignup', 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;
  }
}