From e53ebcfbfdef78b0a74b7256c409e0babec6db46 Mon Sep 17 00:00:00 2001 From: Badtz Date: Sat, 29 Mar 2025 21:48:49 -0700 Subject: [PATCH] config vite for oauth --- src/lib/client-metadata.json | 17 +++++++++++++++ src/vite-env.d.ts | 13 ++++++++++++ vite.config.ts | 41 ++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/lib/client-metadata.json create mode 100644 src/vite-env.d.ts diff --git a/src/lib/client-metadata.json b/src/lib/client-metadata.json new file mode 100644 index 0000000..8ac45a5 --- /dev/null +++ b/src/lib/client-metadata.json @@ -0,0 +1,17 @@ +{ + "client_id": "woach", + "client_name": "Woach", + "client_uri": "https://woach.me", + "redirect_uris": ["https://woach.me/callback"], + "application_type": "web", + "dpop_bound_access_tokens": true, + "grant_types": [ + "authorization_code", + "refresh_token" + ], + "response_types": [ + "code" + ], + "scope": "atproto transition:chat.bsky transition:generic", + "token_endpoint_auth_method": "none" +} diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts new file mode 100644 index 0000000..b6135e6 --- /dev/null +++ b/src/vite-env.d.ts @@ -0,0 +1,13 @@ +/// + +interface ImportMetaEnv { + readonly VITE_DEV_SERVER_PORT?: string; + readonly VITE_CLIENT_URI: string; + readonly VITE_OAUTH_CLIENT_ID: string; + readonly VITE_OAUTH_REDIRECT_URI: string; + readonly VITE_OAUTH_SCOPE: string; +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +} diff --git a/vite.config.ts b/vite.config.ts index 2d35c4f..2a100f4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,44 @@ -import tailwindcss from '@tailwindcss/vite'; import { sveltekit } from '@sveltejs/kit/vite'; +import tailwindcss from '@tailwindcss/vite'; import { defineConfig } from 'vite'; +import metadata from './src/lib/client-metadata.json' with { type: 'json' }; + +const SERVER_HOST = '127.0.0.1'; +const SERVER_PORT = 12520; export default defineConfig({ - plugins: [tailwindcss(), sveltekit()] + server: { + host: SERVER_HOST, + port: SERVER_PORT + }, + plugins: [ + sveltekit(), + tailwindcss(), + { + name: 'oauth-env-injection', + config(_conf, { command }) { + if (command === 'build') { + process.env.VITE_OAUTH_CLIENT_ID = metadata.client_id; + process.env.VITE_OAUTH_REDIRECT_URI = metadata.redirect_uris[0]; + } else { + const redirectUri = (() => { + const url = new URL(metadata.redirect_uris[0]); + return `http://${SERVER_HOST}:${SERVER_PORT}${url.pathname}`; + })(); + + const clientId = + `http://localhost` + + `?redirect_uri=${encodeURIComponent(redirectUri)}` + + `&scope=${encodeURIComponent(metadata.scope)}`; + + process.env.VITE_DEV_SERVER_PORT = '' + SERVER_PORT; + process.env.VITE_OAUTH_CLIENT_ID = clientId; + process.env.VITE_OAUTH_REDIRECT_URI = redirectUri; + } + + process.env.VITE_CLIENT_URI = metadata.client_uri; + process.env.VITE_OAUTH_SCOPE = metadata.scope; + } + } + ] });