Skip to content

@xid-kit/astro

Astro integration with SSR middleware, server helpers, and island client singleton for static and server-rendered Astro sites.

Status

Registry status: UNPUBLISHED. Install this SDK only from the repository source checkout; do not use an external package registry.

Package status is Current package. The Astro integration, SSR middleware, server helpers, and island client singleton are implemented. A real IdP round-trip on production infrastructure is still pending manual verification.

Integration setup

Register xidIntegration in astro.config.mjs. The integration injects server middleware with addMiddleware at order pre, keeps verification material server-side, and exposes only the serializable browser mode configuration required by client islands.

// astro.config.mjs
import { defineConfig } from 'astro/config'
import { xidIntegration } from '@xid-kit/astro'
import node from '@astrojs/node'

const jwtKeyJson = process.env.XID_JWKS_PUBLIC_KEY
if (!jwtKeyJson) throw new Error('Missing XID_JWKS_PUBLIC_KEY')

export default defineConfig({
  output: 'server',
  adapter: node({ mode: 'standalone' }),
  integrations: [
    xidIntegration({
      // This combined SSR setup requires Core routes on the application's exact origin.
      browser: { mode: 'same-origin' },
      jwtKey: JSON.parse(jwtKeyJson),
      issuer: 'https://app.example.com',
      sessionTokenExchange: { endpoint: '/v1/sessions/token' },
      protectedRoutes: ['/dashboard', '/account'],
      signInUrl: '/sign-in',
    }),
  ],
})

Manual middleware (alternative)

// src/middleware.ts
import { sequence } from 'astro:middleware'
import { createXidMiddleware } from '@xid-kit/astro'

export const onRequest = sequence(
  createXidMiddleware({
    jwtKey: JSON.parse(import.meta.env.XID_JWKS_PUBLIC_KEY),
    issuer: 'https://xid.dev',
    sessionTokenExchange: { endpoint: '/v1/sessions/token' },
    protectedRoutes: ['/dashboard', '/account'],
    signInUrl: '/sign-in',
  }),
)

Server-side auth in .astro pages

---
// src/pages/dashboard.astro
import { getAuth, currentUser } from '@xid-kit/astro/server'

const auth = getAuth(Astro.locals)
if (!auth.userId) return Astro.redirect('/sign-in')

const user = await currentUser(Astro.locals, {
  secretKey: import.meta.env.XID_SECRET_KEY,
})
---

<h1>Welcome, {user?.primaryEmailAddress}</h1>

Client island

// src/components/SignOutButton.tsx
import { getClient } from '@xid-kit/astro/client'

export default function SignOutButton() {
  const client = getClient()

  const handleSignOut = async () => {
    const result = await client.signOut()
    if (!result.ok) throw new Error(result.error.message)
    window.location.href = '/'
  }

  return <button onClick={handleSignOut}>Sign out</button>
}

Astro.locals typing

Add the type reference to src/env.d.ts to get full type coverage on Astro.locals.xidAuth.

import '@xid-kit/astro/locals'

Exported API

Export Kind Module
xidIntegration Astro integration factory @xid-kit/astro
createXidMiddleware middleware factory @xid-kit/astro
getAuth, currentUser, xidClient server helpers @xid-kit/astro/server
getClient, initClient, resetClient island client singleton @xid-kit/astro/client

Security notes

  • jwtKey is a JWKS public key; it is safe for networkless verification and does not contain a private key.
  • secretKey (sk_live_xxx) must stay server-side only; never pass it to an island or client bundle.
  • Protected routes redirect via Response.redirect in middleware before any page handler runs; no client-side JS is required.
  • Forward Core opaque cookies only to an exact same-origin session-token endpoint; the value is never verified locally
Navigation

Type to search...

Use arrow keys to navigateEnter to selectEscape to close