---
title: "@xid-kit/astro"
description: "Astro-Integration mit SSR-Middleware, Server-Hilfsfunktionen undIsland-Client-Singleton für statische und serverseitig gerenderteAstro-Seiten."
locale: "de"
---

> Documentation Index
> Fetch the locale documentation index at: https://xid.dev/de/llms.txt
> Use this file to discover all available pages before exploring further.

# @xid-kit/astro

## Zustand

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`. DieIntegration injiziert die Auth-Middleware über `addMiddleware` mitReihenfolge `pre` und stellt den veröffentlichbaren Schlüssel fürClient-Islands bereit.

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

export default defineConfig({
  output: 'server',
  adapter: node({ mode: 'standalone' }),
  integrations: [
xidIntegration({
  publishableKey: import.meta.env.PUBLIC_XID_PK,
  jwtKey: import.meta.env.XID_JWT_KEY,
  protectedRoutes: ['/dashboard', '/account'],
  signInUrl: '/sign-in',
}),
  ],
})
```

## Manuelle Middleware (Alternative)

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

export const onRequest = sequence(
  createXidMiddleware({
jwtKey: import.meta.env.XID_JWT_KEY,
issuer: 'https://xid.dev',
protectedRoutes: ['/dashboard', '/account'],
signInUrl: '/sign-in',
  }),
)
```

## Serverseitige Authentifizierung in .astro-Seiten

```astro
---
// 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

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

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

  const handleSignOut = async () => {
await client.signOut()
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.

```ts
/// <reference path="../node_modules/@xid-kit/astro/src/locals.d.ts" />
```

## 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.
- Re-exportiert [@xid-kit/backend](/de/sdks/backend) `authenticateRequest` für dennetzwerklosen JWT-Prüfpfad.

Source: https://xid.dev/de/sdks/astro/index.mdx
