From 187085ec11fe9ab744a9105dfd258254b2ddd35c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 00:57:52 +0300 Subject: feat: allow querying users by regex --- services/users/users.hooks.ts | 19 ++++++++++++++----- services/users/users.service.ts | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'services/users') diff --git a/services/users/users.hooks.ts b/services/users/users.hooks.ts index ee1ae1c..398d058 100644 --- a/services/users/users.hooks.ts +++ b/services/users/users.hooks.ts @@ -1,3 +1,4 @@ +import _ from 'lodash'; import { hooks } from '@feathersjs/authentication-local'; import { HookContext } from '@feathersjs/feathers'; @@ -8,15 +9,23 @@ const localDispatch = async (context: HookContext): Promise => { return context; }; +const ignoreCaseRegex = async (context: HookContext): Promise => { + 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 + all: hooks.protect('password'), + get: localDispatch, // Protect password from local get's }, before: { - create: [hashPassword], - patch: [hashPassword], - update: [hashPassword] + find: ignoreCaseRegex, + create: hashPassword, + patch: hashPassword, + update: hashPassword } }; diff --git a/services/users/users.service.ts b/services/users/users.service.ts index 08c347d..77eb9fd 100644 --- a/services/users/users.service.ts +++ b/services/users/users.service.ts @@ -3,7 +3,7 @@ import service from 'feathers-mongoose'; import Model from '../../models/users/user.model'; import hooks from './users.hooks'; -const UserService = service({ Model }); +const UserService = service({ Model, whitelist: ['$options', '$regex'] }); export default (app: Application): void => { app.use('/users', UserService); -- cgit v1.2.3 From 2886e64b2d793723d5e8df90725cca1491ba8d6e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 01:26:29 +0300 Subject: refactor: explicitly discard password on get --- services/users/users.hooks.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'services/users') diff --git a/services/users/users.hooks.ts b/services/users/users.hooks.ts index 398d058..64b322a 100644 --- a/services/users/users.hooks.ts +++ b/services/users/users.hooks.ts @@ -1,14 +1,10 @@ import _ from 'lodash'; import { hooks } from '@feathersjs/authentication-local'; +import { discard } from 'feathers-hooks-common'; import { HookContext } from '@feathersjs/feathers'; const hashPassword = hooks.hashPassword('password'); -const localDispatch = async (context: HookContext): Promise => { - context.result = context.dispatch; - return context; -}; - const ignoreCaseRegex = async (context: HookContext): Promise => { context.params.query = _.mapValues(context.params.query, data => { return _.set(data, '$options', 'i'); @@ -19,7 +15,7 @@ const ignoreCaseRegex = async (context: HookContext): Promise => { export default { after: { all: hooks.protect('password'), - get: localDispatch, // Protect password from local get's + get: discard('password'), // Protect password from local get's }, before: { find: ignoreCaseRegex, -- cgit v1.2.3 From bdd8b947e89400606ec1d7e1bd04f3794e942c12 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 28 Jun 2020 04:11:45 +0300 Subject: style: fix linting errors --- services/users/users.hooks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'services/users') diff --git a/services/users/users.hooks.ts b/services/users/users.hooks.ts index 64b322a..48843be 100644 --- a/services/users/users.hooks.ts +++ b/services/users/users.hooks.ts @@ -15,7 +15,7 @@ const ignoreCaseRegex = async (context: HookContext): Promise => { export default { after: { all: hooks.protect('password'), - get: discard('password'), // Protect password from local get's + get: discard('password') // Protect password from local get's }, before: { find: ignoreCaseRegex, -- cgit v1.2.3