Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/roe.dev. This is the code and content for my personal website, built in Nuxt. roe.dev
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081/** * Wire up invitation-link routes from the encrypted `dev.roe.invite` * records on the PDS. * * Each active invite decrypts to a `{ slug, repo }` pair. The slug becomes * a public redirect at `/<slug>` that kicks off the GitHub OAuth dance, and * the repo lives in `runtimeConfig.invites.map` for the callback handler * to read once the OAuth round-trip lands. */import { addServerHandler, createResolver, defineNuxtModule } from 'nuxt/kit'
import { useBuildAirspace } from '../shared/airspace.ts'import { decryptJSON } from '../../server/utils/admin/encryption.ts'import { withCache } from '../shared/build-cache.ts'
/** In dev the (encrypted) invite records are re-read at most hourly. */const DEV_MAX_AGE = 1000 * 60 * 60
export default defineNuxtModule({ meta: { name: 'invites', }, async setup (_, nuxt) { if (nuxt.options._prepare) { nuxt.options.runtimeConfig.invites = { map: {} } return }
const resolver = createResolver(import.meta.url) const gitHubClientId = process.env.NUXT_PUBLIC_GITHUB_CLIENT_ID || nuxt.options.runtimeConfig.public.githubClientId
const map: Record<string, string> = {}
if (process.env.NUXT_PDS_ENCRYPTION_KEY) { try { const records = await withCache({ namespace: 'invites', key: 'records', maxAge: nuxt.options.dev ? DEV_MAX_AGE : 0, stale: nuxt.options.dev, fetch: async () => (await useBuildAirspace().invites.list()) .map(r => ({ uri: r.uri, isActive: r.value.isActive, encrypted: r.value.encrypted })), }) ?? [] for (const r of records) { if (!r.isActive) continue try { const { slug, repo } = decryptJSON<{ slug: string, repo: string }>(r.encrypted) map[slug] = repo } catch (err) { console.warn(`[invites] Failed to decrypt invite ${r.uri}:`, err instanceof Error ? err.message : err) } } } catch (err) { console.warn('[invites] Failed to list invites from PDS:', err instanceof Error ? err.message : err) } } else { console.warn('[invites] NUXT_PDS_ENCRYPTION_KEY not set; invite map will be empty.') }
nuxt.options.runtimeConfig.invites = { map }
if (!gitHubClientId || Object.keys(map).length === 0) return
for (const slug in map) { addServerHandler({ route: '/' + slug, method: 'get', handler: resolver.resolve('./runtime/server/invite.get'), }) }
addServerHandler({ route: '/auth/github/:slug', handler: resolver.resolve('./runtime/server/auth/github/[slug]'), }) },})