aboutsummaryrefslogtreecommitdiff
path: root/src/hooks.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/hooks.ts')
-rw-r--r--src/hooks.ts28
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