diff options
author | mat <github@matdoes.dev> | 2022-03-18 19:32:34 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-03-18 19:32:34 -0500 |
commit | 379531c5182fef14d2c88e08194734d85c9367a8 (patch) | |
tree | de1272385e4c9f435206cd8038931f351a608f54 | |
parent | 713f84cbecdb8318ed5e578a3b7d7074422e950a (diff) | |
download | skyblock-stats-379531c5182fef14d2c88e08194734d85c9367a8.tar.gz skyblock-stats-379531c5182fef14d2c88e08194734d85c9367a8.tar.bz2 skyblock-stats-379531c5182fef14d2c88e08194734d85c9367a8.zip |
Use `platform` for getting env vars in Cloudflare
-rw-r--r-- | src/app.d.ts | 11 | ||||
-rw-r--r-- | src/env.ts | 43 | ||||
-rw-r--r-- | src/hooks.ts | 6 | ||||
-rw-r--r-- | src/routes/login.ts | 11 | ||||
-rw-r--r-- | src/routes/profile/update.ts | 9 | ||||
-rw-r--r-- | src/routes/verify.ts | 9 |
6 files changed, 48 insertions, 41 deletions
diff --git a/src/app.d.ts b/src/app.d.ts index ddaae39..b41fa6f 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -7,7 +7,16 @@ declare namespace App { sid: string | undefined } - // interface Platform {} + + interface Platform { + env: { + DISCORD_CLIENT_ID: string | undefined + SKYBLOCK_STATS_API_KEY: string | undefined + } + context: { + waitUntil(promise: Promise<any>): void + } + } interface Session { sid: string | undefined @@ -1,26 +1,25 @@ -// Cloudflare Workers can't read process.env so we have to do it like this - let skyblockStatsApiKey: string | undefined -try { - skyblockStatsApiKey = process.env.SKYBLOCK_STATS_API_KEY -} catch { - // @ts-ignore - skyblockStatsApiKey = SKYBLOCK_STATS_API_KEY -} -if (!skyblockStatsApiKey) - console.warn('SKYBLOCK_STATS_API_KEY is not set as an environment variable. This is required for logging in with Discord to work. It should be the same as the `key` environment variable in skyblock-api.') - let discordClientId: string | undefined -try { - discordClientId = process.env.DISCORD_CLIENT_ID -} catch { - // @ts-ignore - discordClientId = DISCORD_CLIENT_ID -} -if (!discordClientId) - console.warn('DISCORD_CLIENT_ID is not set as an environment variable. This is required for logging in with Discord to work.') -export { - skyblockStatsApiKey as SKYBLOCK_STATS_API_KEY, - discordClientId as DISCORD_CLIENT_ID, +export default function env(platform?: Readonly<App.Platform>) { + try { + skyblockStatsApiKey = process.env.SKYBLOCK_STATS_API_KEY + } catch { + skyblockStatsApiKey = platform?.env.SKYBLOCK_STATS_API_KEY + } + if (!skyblockStatsApiKey) + console.warn('SKYBLOCK_STATS_API_KEY is not set as an environment variable. This is required for logging in with Discord to work. It should be the same as the `key` environment variable in skyblock-api.') + + try { + discordClientId = process.env.DISCORD_CLIENT_ID + } catch { + discordClientId = platform?.env.DISCORD_CLIENT_ID + } + if (!discordClientId) + console.warn('DISCORD_CLIENT_ID is not set as an environment variable. This is required for logging in with Discord to work.') + + return { + SKYBLOCK_STATS_API_KEY: skyblockStatsApiKey, + DISCORD_CLIENT_ID: discordClientId, + } }
\ No newline at end of file diff --git a/src/hooks.ts b/src/hooks.ts index 151e572..f9df388 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,6 +1,5 @@ import cookie from 'cookie' import type { ExternalFetch, GetSession, Handle } from '@sveltejs/kit' -import { SKYBLOCK_STATS_API_KEY } from './env' export const handle: Handle = async ({ event, resolve }) => { @@ -20,11 +19,6 @@ export const getSession: GetSession = async ({ locals }) => { } 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) - } - const response = await fetch(request) return response diff --git a/src/routes/login.ts b/src/routes/login.ts index f343871..3e247f2 100644 --- a/src/routes/login.ts +++ b/src/routes/login.ts @@ -1,10 +1,13 @@ import type { RequestHandler } from '@sveltejs/kit' -import { DISCORD_CLIENT_ID } from '../env' +import env from '../env' -export const get: RequestHandler = async ({ request }) => { +export const get: RequestHandler = async ({ request, platform }) => { const host = request.headers.get('host') - if (!DISCORD_CLIENT_ID) + + const clientId = env(platform).SKYBLOCK_STATS_API_KEY + + if (!clientId) return { status: 500, headers: { @@ -27,7 +30,7 @@ export const get: RequestHandler = async ({ request }) => { return { status: 303, headers: { - location: `https://discord.com/oauth2/authorize?client_id=${DISCORD_CLIENT_ID}&redirect_uri=${protocol}://${host}%2Floggedin&response_type=code&scope=identify` + location: `https://discord.com/oauth2/authorize?client_id=${clientId}&redirect_uri=${protocol}://${host}%2Floggedin&response_type=code&scope=identify` } } } diff --git a/src/routes/profile/update.ts b/src/routes/profile/update.ts index 5c1e788..8eb108c 100644 --- a/src/routes/profile/update.ts +++ b/src/routes/profile/update.ts @@ -5,7 +5,7 @@ import backgroundFileNames from '../../_backgrounds.json' import donators from '../../_donators.json' import admins from '../../_admins.json' import type { JSONValue } from '@sveltejs/kit/types/internal' -import { SKYBLOCK_STATS_API_KEY } from '../../env' +import env from '../../env' const emojiRegex = /^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])$/ @@ -15,14 +15,15 @@ function isValidEmoji(emoji: string) { } -export const patch: RequestHandler = async ({ request, locals }) => { +export const patch: RequestHandler = async ({ request, locals, platform }) => { if (locals.sid === undefined) { return { body: { ok: false, error: 'You are not logged in.' }, status: 401, } } - if (!SKYBLOCK_STATS_API_KEY) { + const key = env(platform).SKYBLOCK_STATS_API_KEY + if (!key) { return { body: { ok: false, error: 'The SKYBLOCK_STATS_API_KEY environment variable is not set.' }, status: 500, @@ -113,7 +114,7 @@ export const patch: RequestHandler = async ({ request, locals }) => { method: 'POST', headers: { 'Content-Type': 'application/json', - key: SKYBLOCK_STATS_API_KEY + key: key }, body: JSON.stringify(updatedAccount), }).then(r => r.json()) diff --git a/src/routes/verify.ts b/src/routes/verify.ts index c0d2f94..1368344 100644 --- a/src/routes/verify.ts +++ b/src/routes/verify.ts @@ -1,7 +1,7 @@ import { API_URL } from '$lib/api' import type { AccountSchema, CleanUser, SessionSchema } from '$lib/APITypes' import type { RequestHandler } from '@sveltejs/kit' -import { SKYBLOCK_STATS_API_KEY } from '../env' +import env from '../env' function redirect(status: number, location: string) { @@ -13,8 +13,9 @@ function redirect(status: number, location: string) { } } -export const post: RequestHandler = async ({ request, locals }) => { - if (!SKYBLOCK_STATS_API_KEY) { +export const post: RequestHandler = async ({ request, locals, platform }) => { + const key = env(platform).SKYBLOCK_STATS_API_KEY + if (!key) { return redirect(303, `/verify?error=NO_KEY`) } if (locals.sid === undefined) { @@ -65,7 +66,7 @@ export const post: RequestHandler = async ({ request, locals }) => { method: 'POST', headers: { 'Content-Type': 'application/json', - key: SKYBLOCK_STATS_API_KEY + key: key }, body: JSON.stringify(updatedAccount), }).then(r => r.json()) |