Zum Inhalt springen

@xid-kit/svelte

Svelte-5-reaktive Stores und SvelteKit-Server-Hook für Client- undSSR-Authentifizierung.

Als Markdown anzeigen

Zustand

Paketstatus: Aktuelles Paket. Svelte-5-Stores,SvelteKit-Server-Hook und Hilfsprogramme sind implementiert. Ein echterIdP-Round-Trip auf Produktionsinfrastruktur steht noch manuell aus.

Client-Einrichtung

Erstellen Sie Stores im Root-Layout mit createXidStores undsetXidContext. Stores verwenden XidClient.subscribe, was denSvelte-Store-Subscriber-Vertrag erfüllt.

<!-- +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 />

Auth-Status lesen

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

Anmeldung und Abmeldung

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

Rollen- und Berechtigungswächter

<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-Server-Hook

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

Auth in Load-Funktionen lesen

// +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-Typisierung

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

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

Exportierte API

Exportieren Art Modul
XidClient, createXidStores, setXidContext, getXidContext Client-Einrichtung @xid-kit/svelte
buildSignInUrl, executeSignOut, isAllowed Hilfsprogramme @xid-kit/svelte
handleXid, getXidAuth Server-Hook @xid-kit/svelte/server
Navigation

Suchbegriff eingeben...

Mit den Pfeiltasten navigierenEingabetaste zum AuswählenEscape zum Schließen