Zum Inhalt springen

@xid-kit/astro

Astro-Integration mit SSR-Middleware, Server-Hilfsfunktionen undIsland-Client-Singleton für statische und serverseitig gerenderteAstro-Seiten.

Als Markdown anzeigen

Zustand

Registry-Status: UNPUBLISHED. Installieren Sie dieses SDK nur aus einem Checkout des Repository-Quellcodes; verwenden Sie keine externe Paket-Registry.

Paketstatus: Aktuelles Paket. Die Astro-Integration,SSR-Middleware, Server-Hilfsfunktionen und Island-Client-Singleton sindimplementiert. Ein echter IdP-Round-Trip auf Produktionsinfrastruktursteht noch manuell aus.

Integrations-Einrichtung

Registrieren Sie xidIntegration in astro.config.mjs. Die Integration injiziert Server-Middleware mit addMiddleware in der Reihenfolge pre, hält Verifizierungsmaterial serverseitig und stellt nur die serialisierbare Browsermodus-Konfiguration bereit, die von Client Islands benötigt wird.

// 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',
    }),
  ],
})

Manuelle 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',
  }),
)

Serverseitige Authentifizierung in .astro-Seiten

---
// 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-Typisierung

Fügen Sie die Typreferenz in src/env.d.ts hinzu, um vollständigeTypabdeckung für Astro.locals.xidAuth zu erhalten.

import '@xid-kit/astro/locals'

Exportierte API

Exportieren Art Modul
xidIntegration Astro-Integrationsfabrik @xid-kit/astro
createXidMiddleware Middleware-Factory @xid-kit/astro
getAuth, currentUser, xidClient Server-Hilfsfunktionen @xid-kit/astro/server
getClient, initClient, resetClient Island-Client-Singleton @xid-kit/astro/client

Sicherheitshinweise

  • jwtKey ist ein öffentlicher JWKS-Schlüssel; er eignet sich für dienetzwerklose Prüfung und enthält keinen privaten Schlüssel.
  • secretKey (sk_live_xxx) muss ausschließlich serverseitigbleiben; übergeben Sie ihn nie an eine Island oder ein Client-Bundle.
  • Geschützte Routen leiten über Response.redirect in der Middlewareweiter, bevor ein Seitenhandler ausgeführt wird; kein clientseitiges JSerforderlich.
  • Leitet opaque Core-Cookies nur an einen Session-Token-Endpoint mit exakt gleicher Origin weiter; der Wert wird nie lokal geprüft
Navigation

Suchbegriff eingeben...

Mit den Pfeiltasten navigierenEingabetaste zum AuswählenEscape zum Schließen