diff options
author | mat <github@matdoes.dev> | 2022-03-17 14:27:36 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-03-17 14:27:36 -0500 |
commit | 557423e887a363d0d1be1dfc6db613f83fd7cec0 (patch) | |
tree | 08796b7de8bff9a9de1f438c5bb45f2b62d43410 /src/hooks.ts | |
parent | e6be291bd6787199f644b263c28c6d2c8b9b1d74 (diff) | |
download | skyblock-stats-557423e887a363d0d1be1dfc6db613f83fd7cec0.tar.gz skyblock-stats-557423e887a363d0d1be1dfc6db613f83fd7cec0.tar.bz2 skyblock-stats-557423e887a363d0d1be1dfc6db613f83fd7cec0.zip |
add logging in
Diffstat (limited to 'src/hooks.ts')
-rw-r--r-- | src/hooks.ts | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/hooks.ts b/src/hooks.ts index 9cd6ca4..5b3464c 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,11 +1,16 @@ import cookie from 'cookie' -import { v4 as uuid } from '@lukeed/uuid' -import type { Handle } from '@sveltejs/kit' +import type { ExternalFetch, GetSession, Handle } from '@sveltejs/kit' + +const SKYBLOCK_STATS_API_KEY = process.env.SKYBLOCK_STATS_API_KEY +if (!SKYBLOCK_STATS_API_KEY) + console.warn('SKYBLOCK_STATS_API_KEY is not set as an environment variable. This is required for logging in with Skyblock Stats to work. It should be the same as the `key` environment variable in skyblock-api.') export const handle: Handle = async ({ event, resolve }) => { - // const cookies = cookie.parse(event.request.headers.get('cookie') || ''); + const cookies = cookie.parse(event.request.headers.get('cookie') || '') // event.locals.userid = cookies.userid || uuid(); + event.locals.sid = cookies.sid + const response = await resolve(event) // if (!cookies.userid) { @@ -22,3 +27,20 @@ export const handle: Handle = async ({ event, resolve }) => { return response } + +export const getSession: GetSession = async ({ locals }) => { + return { + sid: locals.sid + } +} + +export const externalFetch: ExternalFetch = async (request) => { + if (SKYBLOCK_STATS_API_KEY && request.url.startsWith('https://skyblock-api.matdoes.dev/')) { + // add the key as a header + request.headers.set('key', SKYBLOCK_STATS_API_KEY) + } + + console.log('request', request.url) + + return fetch(request) +}
\ No newline at end of file |