---
title: "@xid-kit/svelte"
description: "Svelte 5 リアクティブストアと SvelteKit サーバーフックによるクライアントおよび SSR 認証をサポート。"
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/svelte

## 状態

パッケージステータス：**現行パッケージ**。Svelte 5 ストア、SvelteKit サーバーフック、ヘルパーユーティリティが実装済みです。本番インフラでの実際の IdP ラウンドトリップはまだ手動検証待ちです。

## クライアントセットアップ

ルートレイアウトで `createXidStores` と `setXidContext` を使ってストアを作成します。ストアは Svelte ストアのサブスクライバーコントラクトを満たす `XidClient.subscribe` を使用します。

```svelte
<!-- +layout.svelte -->
<script lang="ts">
  import { setContext, onMount } from 'svelte'
  import { XidClient, createXidStores, setXidContext } from '@xid-kit/svelte'

  const client = new XidClient({ apiUrl: 'https://acme.xid.dev' })
  const stores = createXidStores(client)
  setXidContext(setContext, stores)

  onMount(() => {
const ac = new AbortController()
void client.load({ signal: ac.signal })
return () => ac.abort()
  })
</script>
<slot />
```

## 認証状態の読み取り

```svelte
<!-- Any child component -->
<script lang="ts">
  import { getContext } from 'svelte'
  import { getXidContext } from '@xid-kit/svelte'

  const { auth, user } = getXidContext(getContext)
  // $auth: { isLoaded, isSignedIn, userId, sessionId, session }
  // $user: discriminated union on isLoaded / isSignedIn / user
</script>

{#if !$auth.isLoaded}
  <p>Loading...</p>
{:else if $auth.isSignedIn}
  <p>Hello, {$user.isSignedIn ? $user.user.fullName : ''}</p>
{:else}
  <p>Not signed in</p>
{/if}
```

## サインインとサインアウト

```svelte
<script lang="ts">
  import { getContext } from 'svelte'
  import { getXidContext, buildSignInUrl, executeSignOut } from '@xid-kit/svelte'

  const { client } = getXidContext(getContext)

  function handleSignIn() {
window.location.assign(buildSignInUrl('/sign-in', window.location.href))
  }

  async function handleSignOut() {
await executeSignOut(client, { redirectUrl: '/' })
  }
</script>
```

## ロールと権限ガード

```svelte
<script lang="ts">
  import { getContext } from 'svelte'
  import { getXidContext, isAllowed } from '@xid-kit/svelte'

  const { state } = getXidContext(getContext)
</script>

{#if isAllowed($state, { role: 'org:admin' })}
  <AdminPanel />
{/if}

{#if isAllowed($state, { permission: 'org:member:write' })}
  <EditButton />
{/if}
```

## SvelteKit サーバーフック

```ts
// src/hooks.server.ts
import { handleXid } from '@xid-kit/svelte/server'

export const handle = handleXid({
  jwtKey: JSON.parse(process.env.XID_JWT_KEY!),
  issuer: 'https://xid.dev',
  protectedRoutes: ['/dashboard', '/account'],
  signInUrl: '/sign-in',
})
```

## load 関数での認証読み取り

```ts
// +page.server.ts
import { redirect } from '@sveltejs/kit'
import { getXidAuth } from '@xid-kit/svelte/server'
import type { PageServerLoad } from './$types'

export const load: PageServerLoad = async ({ locals }) => {
  const auth = getXidAuth(locals)
  if (!auth.userId) throw redirect(303, '/sign-in')
  return { userId: auth.userId, orgId: auth.orgId }
}
```

## App.Locals の型定義

```ts
// src/app.d.ts
import type { AuthResult } from '@xid-kit/svelte/server'

declare global {
  namespace App {
interface Locals {
  xidAuth: AuthResult
}
  }
}
export {}
```

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

| エクスポート | 種別 | モジュール |
| --- | --- | --- |
| `XidClient, createXidStores, setXidContext, getXidContext` | クライアントセットアップ | `@xid-kit/svelte` |
| `buildSignInUrl, executeSignOut, isAllowed` | ユーティリティ | `@xid-kit/svelte` |
| `handleXid, getXidAuth` | サーバーフック | `@xid-kit/svelte/server` |

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