aboutsummaryrefslogtreecommitdiff
path: root/hooks
diff options
context:
space:
mode:
Diffstat (limited to 'hooks')
-rw-r--r--hooks/handleErrors.ts11
-rw-r--r--hooks/logging.ts14
-rw-r--r--hooks/requireAuth.ts5
3 files changed, 29 insertions, 1 deletions
diff --git a/hooks/handleErrors.ts b/hooks/handleErrors.ts
new file mode 100644
index 0000000..2a3c728
--- /dev/null
+++ b/hooks/handleErrors.ts
@@ -0,0 +1,11 @@
+import { HookContext } from '@feathersjs/feathers';
+import logger from '../logger';
+
+
+export default async (context: HookContext): Promise<HookContext> => {
+ context.result = context.error.message;
+ context.statusCode = context.error.code;
+ logger.error(context.error);
+ return context;
+};
+
diff --git a/hooks/logging.ts b/hooks/logging.ts
new file mode 100644
index 0000000..8babe9a
--- /dev/null
+++ b/hooks/logging.ts
@@ -0,0 +1,14 @@
+import { HookContext } from '@feathersjs/feathers';
+import logger from '../logger';
+
+export default async (context: HookContext): Promise<HookContext> => {
+ if (context.params.provider) {
+ const { method, path, id } = context;
+ const message = `${method.toUpperCase()}: /${path}/${id || ''}`;
+ const username = context.params.user?.username || 'anonymous';
+
+ logger.log(`${message} ${username}`);
+ }
+ return context;
+};
+
diff --git a/hooks/requireAuth.ts b/hooks/requireAuth.ts
index a7b0e96..52e8974 100644
--- a/hooks/requireAuth.ts
+++ b/hooks/requireAuth.ts
@@ -1,7 +1,10 @@
+import { NotAuthenticated } from '@feathersjs/errors';
import { HookContext } from '@feathersjs/feathers';
export default async (context: HookContext): Promise<HookContext> => {
- if (!context.params.user) throw new Error('This endpoint requires auth!');
+ if (!context.params.authenticated) {
+ throw new NotAuthenticated('This endpoint requires auth!');
+ }
return context;
};