From 0a78ac48ed5ae37f88e5f36194953424a5bc18a0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 27 Jun 2020 00:53:48 +0300 Subject: refactor: move sortByDate hook to global hooks --- hooks/sortByDate.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 hooks/sortByDate.ts (limited to 'hooks') diff --git a/hooks/sortByDate.ts b/hooks/sortByDate.ts new file mode 100644 index 0000000..4a20920 --- /dev/null +++ b/hooks/sortByDate.ts @@ -0,0 +1,8 @@ +import _ from 'lodash'; +import { HookContext } from '@feathersjs/feathers'; + +export default async (context: HookContext): Promise => { + _.set(context, 'params.query.$sort', { createdAt: - 1}); + return context; +} + -- cgit v1.2.3 From a8c74bf2c5b8a8cc0bfd0f1d082eb3bf8357f6bc Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 27 Jun 2020 01:26:03 +0300 Subject: style: fix linting errors --- hooks/sortByDate.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'hooks') diff --git a/hooks/sortByDate.ts b/hooks/sortByDate.ts index 4a20920..9dd1222 100644 --- a/hooks/sortByDate.ts +++ b/hooks/sortByDate.ts @@ -2,7 +2,7 @@ import _ from 'lodash'; import { HookContext } from '@feathersjs/feathers'; export default async (context: HookContext): Promise => { - _.set(context, 'params.query.$sort', { createdAt: - 1}); + _.set(context, 'params.query.$sort', { createdAt: -1 }); return context; -} +}; -- cgit v1.2.3 From 69b212128435cab2919f642fd8352b0b6cf93f83 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 27 Jun 2020 16:25:59 +0300 Subject: feat: add isAuthenticated hook --- hooks/isAuthenticated.ts | 9 +++++++++ hooks/requireAuth.ts | 13 ++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 hooks/isAuthenticated.ts (limited to 'hooks') diff --git a/hooks/isAuthenticated.ts b/hooks/isAuthenticated.ts new file mode 100644 index 0000000..d0da8b6 --- /dev/null +++ b/hooks/isAuthenticated.ts @@ -0,0 +1,9 @@ +import { HookContext } from '@feathersjs/feathers'; +import { authenticate } from '@feathersjs/authentication'; + + +export default async (context: HookContext): Promise => { + console.log(context.params.authenticated); + return context.params.authenticated || false; +}; + diff --git a/hooks/requireAuth.ts b/hooks/requireAuth.ts index 52e8974..ac46e22 100644 --- a/hooks/requireAuth.ts +++ b/hooks/requireAuth.ts @@ -1,10 +1,9 @@ +import { iff, isNot } from 'feathers-hooks-common'; import { NotAuthenticated } from '@feathersjs/errors'; -import { HookContext } from '@feathersjs/feathers'; +import isAuthenticated from './isAuthenticated'; -export default async (context: HookContext): Promise => { - if (!context.params.authenticated) { - throw new NotAuthenticated('This endpoint requires auth!'); - } - return context; -}; +export default iff( + isNot(isAuthenticated), + () => { throw new NotAuthenticated('This endpoint requires auth!') } +); -- cgit v1.2.3 From 0a6a6be1b43635c60e76669ac2a7ee8581d9b183 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 27 Jun 2020 16:40:07 +0300 Subject: style: fix linting errors --- hooks/isAuthenticated.ts | 2 -- hooks/requireAuth.ts | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'hooks') diff --git a/hooks/isAuthenticated.ts b/hooks/isAuthenticated.ts index d0da8b6..50290c8 100644 --- a/hooks/isAuthenticated.ts +++ b/hooks/isAuthenticated.ts @@ -1,6 +1,4 @@ import { HookContext } from '@feathersjs/feathers'; -import { authenticate } from '@feathersjs/authentication'; - export default async (context: HookContext): Promise => { console.log(context.params.authenticated); diff --git a/hooks/requireAuth.ts b/hooks/requireAuth.ts index ac46e22..ba08f57 100644 --- a/hooks/requireAuth.ts +++ b/hooks/requireAuth.ts @@ -4,6 +4,8 @@ import isAuthenticated from './isAuthenticated'; export default iff( isNot(isAuthenticated), - () => { throw new NotAuthenticated('This endpoint requires auth!') } + () => { + throw new NotAuthenticated('This endpoint requires auth!'); + } ); -- cgit v1.2.3