コンテンツへ移動

@xid-kit/astro

静的およびサーバーレンダリングされた Astro サイト向け SSR ミドルウェア、サーバーヘルパー、アイランドクライアントシングルトンを含む Astro 統合。

Markdown で表示

状態

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

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

統合セットアップ

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

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

手動ミドルウェア(代替)

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

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

クライアントアイランド

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

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

import '@xid-kit/astro/locals'

エクスポートされた 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 公開鍵です。ネットワークレス検証に安全で、秘密鍵を含みません。
  • secretKeysk_live_xxx)はサーバーサイド専用にしてください。アイランドやクライアントバンドルには渡さないでください。
  • 保護されたルートはページハンドラーが実行される前にミドルウェアで Response.redirect 経由でリダイレクトします。クライアントサイド JS は不要です。
  • Core の opaque cookie は exact same-origin の session-token endpoint にのみ転送され、その値をローカルで検証することはありません
ナビゲーション

入力して検索...

矢印キーで移動Enter キーで選択Escape キーで閉じる