---
title: "@xid-kit/svelte"
description: "Stores réactifs Svelte 5 et hook serveur SvelteKit pour l'authentificationclient et SSR."
locale: "fr"
---

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

# @xid-kit/svelte

## Statut

Le statut du package est **Package actuel**. Les stores Svelte 5, lehook serveur SvelteKit et les utilitaires auxiliaires sont implémentés. Unaller-retour réel avec un IdP sur une infrastructure de production est enattente de vérification manuelle.

## Configuration du client

Créez des stores dans la mise en page racine avec `createXidStores`et `setXidContext`. Les stores utilisent `XidClient.subscribe`,ce qui satisfait le contrat d'abonnement des stores Svelte.

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

## Lecture de l'état d'authentification

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

## Connexion et déconnexion

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

## Guard de rôle et permission

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

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

## Lecture de l'authentification dans les fonctions 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 }
}
```

## Typage de 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 exportée

| Exporter | Type | Module |
| --- | --- | --- |
| `XidClient, createXidStores, setXidContext, getXidContext` | configuration du client | `@xid-kit/svelte` |
| `buildSignInUrl, executeSignOut, isAllowed` | utilitaires | `@xid-kit/svelte` |
| `handleXid, getXidAuth` | hook serveur | `@xid-kit/svelte/server` |

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