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

> Documentation Index
> Fetch the relevant documentation index at: https://vonvon.id/ja/sdks/llms.txt
> Use this file to discover all available pages before exploring further.

# @vonvon-kit/astro

## 状態

Registry 状態: UNPUBLISHED。この SDK はリポジトリのソース checkout からのみインストールし、外部 package registry は使用しないでください。

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

## 統合セットアップ

`astro.config.mjs` に `vonvonIntegration` を登録します。この integration は `addMiddleware` を使用して `pre` の順序で server middleware を注入し、検証用マテリアルをサーバー側に保持したまま、client islands に必要なシリアライズ可能な browser mode configuration のみを公開します。

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

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

export default defineConfig({
  output: 'server',
  adapter: node({ mode: 'standalone' }),
  integrations: [
vonvonIntegration({
  // 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',
}),
  ],
})
```

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

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

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

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

```astro
---
// src/pages/dashboard.astro
import { getAuth, currentUser } from '@vonvon-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.VONVON_SECRET_KEY,
})
---

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

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

```tsx
// src/components/SignOutButton.tsx
import { getClient } from '@vonvon-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 の型定義

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

```ts
import '@vonvon-kit/astro/locals'
```

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

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

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

- `jwtKey` は JWKS 公開鍵です。ネットワークレス検証に安全で、秘密鍵を含みません。
- `secretKey`（`sk_live_xxx`）はサーバーサイド専用にしてください。アイランドやクライアントバンドルには渡さないでください。
- 保護されたルートはページハンドラーが実行される前にミドルウェアで `Response.redirect` 経由でリダイレクトします。クライアントサイド JS は不要です。
- Core の opaque cookie は exact same-origin の session-token endpoint にのみ転送され、その値をローカルで検証することはありません

Source: https://vonvon.id/ja/sdks/astro/index.mdx
