---
title: "@xid-kit/astro"
description: "静的およびサーバーレンダリングされた Astro サイト向け SSR ミドルウェア、サーバーヘルパー、アイランドクライアントシングルトンを含む Astro 統合。"
locale: "ja"
---

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

# @xid-kit/astro

## 状態

パッケージステータス：**現行パッケージ**。Astro 統合、SSR ミドルウェア、サーバーヘルパー、アイランドクライアントシングルトンが実装済みです。本番インフラでの実際の IdP ラウンドトリップはまだ手動検証待ちです。

## 統合セットアップ

`astro.config.mjs` に `xidIntegration` を登録します。統合は `addMiddleware` 経由で順序 `pre` に認証ミドルウェアを注入し、クライアントアイランドに公開可能なキーを提供します。

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

## 手動ミドルウェア（代替）

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

## .astro ページでのサーバーサイド認証

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

## クライアントアイランド

```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 の型定義

`Astro.locals.xidAuth` の完全な型カバレッジを得るには `src/env.d.ts` に型参照を追加します。

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

## エクスポートされた API

| エクスポート | 種別 | モジュール |
| --- | --- | --- |
| `xidIntegration` | Astro 統合ファクトリー | `@xid-kit/astro` |
| `createXidMiddleware` | ミドルウェアファクトリー | `@xid-kit/astro` |
| `getAuth, currentUser, xidClient` | サーバーヘルパー | `@xid-kit/astro/server` |
| `getClient, initClient, resetClient` | アイランドクライアントシングルトン | `@xid-kit/astro/client` |

## セキュリティに関する注意事項

- `jwtKey` は JWKS 公開鍵です。ネットワークレス検証に安全で、秘密鍵を含みません。
- `secretKey`（`sk_live_xxx`）はサーバーサイド専用にしてください。アイランドやクライアントバンドルには渡さないでください。
- 保護されたルートはページハンドラーが実行される前にミドルウェアで `Response.redirect` 経由でリダイレクトします。クライアントサイド JS は不要です。
- ネットワークレス JWT 検証パス用の [@xid-kit/backend](/ja/sdks/backend) `authenticateRequest` を再エクスポートします。

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