aboutsummaryrefslogtreecommitdiff
path: root/services/users/users.hooks.ts
blob: 398d05815d22d7a22a7de91d5a3a18aecf28dbef (plain)
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
import _ from 'lodash';
import { hooks } from '@feathersjs/authentication-local';
import { HookContext } from '@feathersjs/feathers';

const hashPassword = hooks.hashPassword('password');

const localDispatch = async (context: HookContext): Promise<HookContext> => {
  context.result = context.dispatch;
  return context;
};

const ignoreCaseRegex = async (context: HookContext): Promise<HookContext> => {
  context.params.query = _.mapValues(context.params.query, data => {
    return _.set(data, '$options', 'i');
  });
  return context;
};

export default {
  after: {
    all: hooks.protect('password'),
    get: localDispatch, // Protect password from local get's
  },
  before: {
    find: ignoreCaseRegex,
    create: hashPassword,
    patch: hashPassword,
    update: hashPassword
  }
};