diff options
author | Ubuntu <github@matdoes.dev> | 2022-02-14 16:33:38 +0000 |
---|---|---|
committer | Ubuntu <github@matdoes.dev> | 2022-02-14 16:33:38 +0000 |
commit | 9f28b6d9160fee5eff92d1d9849191f2f12faeab (patch) | |
tree | 4ae0ce31df50b0d34a240da1be9bdfa0d4bb6a66 /src/hooks.ts | |
download | skyblock-stats-9f28b6d9160fee5eff92d1d9849191f2f12faeab.tar.gz skyblock-stats-9f28b6d9160fee5eff92d1d9849191f2f12faeab.tar.bz2 skyblock-stats-9f28b6d9160fee5eff92d1d9849191f2f12faeab.zip |
Initial commit
Diffstat (limited to 'src/hooks.ts')
-rw-r--r-- | src/hooks.ts | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/hooks.ts b/src/hooks.ts new file mode 100644 index 0000000..d767555 --- /dev/null +++ b/src/hooks.ts @@ -0,0 +1,24 @@ +import cookie from 'cookie'; +import { v4 as uuid } from '@lukeed/uuid'; +import type { Handle } from '@sveltejs/kit'; + +export const handle: Handle = async ({ event, resolve }) => { + const cookies = cookie.parse(event.request.headers.get('cookie') || ''); + event.locals.userid = cookies.userid || uuid(); + + const response = await resolve(event); + + if (!cookies.userid) { + // if this is the first time the user has visited this app, + // set a cookie so that we recognise them when they return + response.headers.set( + 'set-cookie', + cookie.serialize('userid', event.locals.userid, { + path: '/', + httpOnly: true + }) + ); + } + + return response; +}; |