Skip to content

Commit 8f48be5

Browse files
committed
feat: ensure users security
1 parent 7b24cea commit 8f48be5

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

services/users/users.hooks.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import _ from 'lodash';
22
import { hooks } from '@feathersjs/authentication-local';
3-
import { discard } from 'feathers-hooks-common';
3+
import { discard, disallow } from 'feathers-hooks-common';
44
import { HookContext } from '@feathersjs/feathers';
5+
import { NotAuthenticated } from '@feathersjs/errors';
6+
import requireAuth from '../../hooks/requireAuth';
57

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

@@ -12,6 +14,13 @@ const ignoreCaseRegex = async (context: HookContext): Promise<HookContext> => {
1214
return context;
1315
};
1416

17+
const compareUser = async (context: HookContext): Promise<HookContext> => {
18+
if(context.arguments[0] != context.params.user._id) {
19+
throw new NotAuthenticated('You can only PATCH/UPDATE your own user!');
20+
}
21+
return context;
22+
}
23+
1524
export default {
1625
after: {
1726
all: hooks.protect('password'),
@@ -20,8 +29,9 @@ export default {
2029
before: {
2130
find: ignoreCaseRegex,
2231
create: hashPassword,
23-
patch: hashPassword,
24-
update: hashPassword
32+
patch: [hashPassword, requireAuth, compareUser],
33+
update: [hashPassword, requireAuth, compareUser],
34+
remove: disallow('external')
2535
}
2636
};
2737

0 commit comments

Comments
 (0)