aboutsummaryrefslogtreecommitdiffstats
path: root/src/hooks
diff options
context:
space:
mode:
authorAdrien Hopkins <[email protected]>2022-08-02 13:13:27 -0500
committerAdrien Hopkins <[email protected]>2022-08-02 13:13:27 -0500
commitc21ee41b206db79b2157d8743b0473a094448dfb (patch)
treefac1a595e451c0b32ceedfe4fb70855270456377 /src/hooks
parent3b216dd6c170a7d71d21a391098c554008c8bdb4 (diff)
parent05f5001fc7d325e31d9c777be11343604c72fbfd (diff)
downloadcoinos-ui-c21ee41b206db79b2157d8743b0473a094448dfb.tar.gz
coinos-ui-c21ee41b206db79b2157d8743b0473a094448dfb.zip
Merge branch 'master' into i18n
Diffstat (limited to 'src/hooks')
-rw-r--r--src/hooks/index.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/hooks/index.js b/src/hooks/index.js
new file mode 100644
index 00000000..2284d88d
--- /dev/null
+++ b/src/hooks/index.js
@@ -0,0 +1,25 @@
+import cookie from 'cookie';
+import { get } from '$lib/utils';
+import { protectedRoutes } from '$lib/utils';
+
+export async function handle({ event, resolve }) {
+ let {
+ params,
+ request: { headers }
+ } = event;
+
+ const cookies = cookie.parse(headers.get('cookie') || '');
+ let { token } = cookies;
+ event.locals = { token };
+
+ if (protectedRoutes.find((p) => event.url.pathname.match(p))) {
+ let user = await get('/me', { accept: 'application/json', authorization: `Bearer ${token}` });
+ if (user.username !== params.username) throw new Error('user not found');
+ }
+
+ return resolve(event);
+}
+
+export const getSession = ({ locals: { token } }) => {
+ return { token };
+};