All files / src/hooks remove-verification.ts

100% Statements 25/25
93.75% Branches 15/16
100% Functions 3/3
100% Lines 24/24

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  1x       1x     7x 7x   6x 6x 5x 5x   5x 6x   1x 1x 1x       6x 5x 5x 5x 5x 5x 5x 5x 5x         5x 5x      
import { HookContext } from '@feathersjs/feathers';
import { checkContext, getItems, replaceItems } from 'feathers-hooks-common';
 
import type { User } from '../types';
 
export default function removeVerification (
  ifReturnTokens?: boolean
): ((context: HookContext) => HookContext) {
  return (context: HookContext): HookContext => {
    checkContext(context, 'after');
    // Retrieve the items from the hook
    const items: User | User[] = getItems(context);
    if (!items) return;
    const isArray = Array.isArray(items);
    const users = ((isArray) ? items : [items]) as User[];
 
    users.forEach(user => {
      if (!('isVerified' in user) && context.method === 'create') {
        /* eslint-disable no-console */
        console.warn('Property isVerified not found in user properties. (removeVerification)');
        console.warn('Have you added authManagement\'s properties to your model? (Refer to README.md)');
        console.warn('Have you added the addVerification hook on users::create?');
        /* eslint-enable */
      }
 
      if (context.params.provider && user) { // noop if initiated by server
        delete user.verifyExpires;
        delete user.resetExpires;
        delete user.verifyChanges;
        Eif (!ifReturnTokens) {
          delete user.verifyToken;
          delete user.verifyShortToken;
          delete user.resetToken;
          delete user.resetShortToken;
        }
      }
    });
    // Replace the items within the hook
    replaceItems(context, isArray ? users : users[0]);
    return context;
  };
}