diff --git a/.env.example b/.env.example index 6ae7bb8..06a62cc 100644 --- a/.env.example +++ b/.env.example @@ -9,7 +9,7 @@ NUXT_OAUTH_DISCORD_CLIENT_SECRET= NUXT_DISCORD_BOT_TOKEN= # Public -NUXT_PUBLIC_APP_URL=http://localhost:3000 +NUXT_PUBLIC_APP_URL=http://127.0.0.1:3000 NUXT_PUBLIC_DISCORD_CLIENT_ID= # Redis KV store (production only — dev uses fs driver) diff --git a/server/plugins/atproto-client.ts b/server/plugins/atproto-client.ts index 6584c73..465b1ef 100644 --- a/server/plugins/atproto-client.ts +++ b/server/plugins/atproto-client.ts @@ -6,13 +6,15 @@ import type { NodeOAuthClient } from '@atproto/oauth-client-node' export default defineNitroPlugin((nitroApp) => { const client = createAtprotoClient() - nitroApp.hooks.hook('request', (event) => { - event.context.atprotoClient = client - }) + if (client) { + nitroApp.hooks.hook('request', (event) => { + event.context.atprotoClient = client + }) + } }) declare module 'h3' { interface H3EventContext { - atprotoClient: NodeOAuthClient + atprotoClient?: NodeOAuthClient } } diff --git a/server/routes/auth/bluesky.get.ts b/server/routes/auth/bluesky.get.ts index eeda93e..33f310b 100644 --- a/server/routes/auth/bluesky.get.ts +++ b/server/routes/auth/bluesky.get.ts @@ -1,6 +1,9 @@ export default defineEventHandler(async (event) => { const query = getQuery(event) const client = event.context.atprotoClient + if (!client) { + throw createError({ statusCode: 503, message: 'AT Proto OAuth is not configured' }) + } // Step 1: No code = initial request, start the OAuth flow if (!query.code) { diff --git a/server/utils/atproto-stores.ts b/server/utils/atproto-stores.ts index 0cb5858..bd2caaa 100644 --- a/server/utils/atproto-stores.ts +++ b/server/utils/atproto-stores.ts @@ -82,7 +82,7 @@ export async function distributedLock(key: string, fn: () => T | PromiseLike< return promise } -export function createAtprotoClient(): NodeOAuthClient { +export function createAtprotoClient(): NodeOAuthClient | null { const config = useRuntimeConfig() const blueskyConfig = config.oauth.bluesky as { scope?: string[] @@ -90,6 +90,12 @@ export function createAtprotoClient(): NodeOAuthClient { } const appUrl = config.public.appUrl as string + // In production builds without a proper HTTPS appUrl (e.g. CI), skip client + // creation as AT Proto OAuth validation will reject the metadata. + if (!import.meta.dev && !appUrl.startsWith('https://')) { + return null + } + const scopes = [...new Set(['atproto', ...blueskyConfig.scope ?? []])] const scope = scopes.join(' ') const redirectUri = `${appUrl}${blueskyConfig.redirectUris?.[0] ?? '/auth/bluesky'}` diff --git a/server/utils/atproto.ts b/server/utils/atproto.ts index 464526d..1b8f40d 100644 --- a/server/utils/atproto.ts +++ b/server/utils/atproto.ts @@ -27,6 +27,9 @@ const RSVP_STATUS_MAP: Record = { export async function getAtprotoAgent(event: H3Event, did: string): Promise { const client = event.context.atprotoClient + if (!client) { + throw new Error('AT Proto OAuth client is not available') + } try { const session = await client.restore(did) return new Agent(session)