Skip to content

Components

Overview

@authrim/sveltekit/components provides five core components for common authentication patterns:

ComponentPurpose
AuthProviderContext provider — wraps your app with auth instance
ProtectedRouteRoute guard — redirects unauthenticated users
SignInButtonPasskey or social sign-in with built-in state
SignOutButtonSign out with loading state
UserProfileDisplay current user info

AuthProvider

Wraps your app and provides the auth client via Svelte context. Also handles SSR session synchronization.

<script lang="ts">
import { AuthProvider } from '@authrim/sveltekit/components';
import type { AuthrimClient } from '@authrim/sveltekit';
let { data, children } = $props();
let auth: AuthrimClient | null = $state(null);
// Initialize auth client...
</script>
{#if auth}
<AuthProvider
{auth}
initialSession={data.auth?.session ?? null}
initialUser={data.auth?.user ?? null}
>
{@render children()}
</AuthProvider>
{/if}

Props

PropTypeDefaultDescription
authAuthrimClientrequiredAuthrim client instance
initialSessionSession | nullnullSSR session for hydration
initialUserUser | nullnullSSR user for hydration

Behavior

  1. Sets auth context via setAuthContext(auth) — makes getAuthContext() available to all children
  2. Calls auth._syncFromSSR() synchronously to set stores before first render
  3. On mount, optionally revalidates the session in the background
  4. On destroy, calls auth.destroy() to clean up event listeners and timers

Context API

Inside any child component of AuthProvider:

import { getAuthContext } from '@authrim/sveltekit';
const auth = getAuthContext();
// Access auth.passkey, auth.stores, auth.on(), etc.

ProtectedRoute

Shows content only when the user is authenticated. Optionally redirects to a login page.

<script lang="ts">
import { ProtectedRoute } from '@authrim/sveltekit/components';
</script>
<ProtectedRoute redirectTo="/login">
<h1>Dashboard</h1>
<p>This is protected content.</p>
</ProtectedRoute>

Props

PropTypeDefaultDescription
redirectTostring | undefinedRedirect URL for unauthenticated users
includeReturnPathbooleantrueAppend current path as query param
returnPathParamstring'redirectTo'Query parameter name
classstring''CSS class

Slots

SlotWhen RenderedProps
DefaultUser is authenticated
loadingSession check in progress
unauthenticatedNot authenticated and no redirectTo
<ProtectedRoute redirectTo="/login">
<!-- Authenticated content -->
<h1>Welcome back</h1>
{#snippet loading()}
<div class="spinner">Checking session...</div>
{/snippet}
{#snippet unauthenticated()}
<p>Please sign in to continue.</p>
{/snippet}
</ProtectedRoute>

SignInButton

A button that handles passkey or social sign-in with built-in loading state.

<script lang="ts">
import { SignInButton } from '@authrim/sveltekit/components';
import { goto } from '$app/navigation';
</script>
<SignInButton
method="passkey"
onsuccess={() => goto('/')}
onerror={(e) => console.error(e.detail.message)}
/>
<SignInButton
method="social"
provider="google"
onsuccess={() => goto('/')}
/>

Props

PropTypeDefaultDescription
method'passkey' | 'social''passkey'Authentication method
providerSocialProviderRequired when method='social'
disabledbooleanfalseDisabled state
classstring''CSS class

Events

EventPayloadDescription
success{ session: Session; user: User }Successful sign-in
errorAuthErrorSign-in failed
loadingAuthLoadingStateLoading state changed

Custom Content

<SignInButton method="passkey">
🔑 Sign in with your passkey
</SignInButton>

SignOutButton

A button that handles sign out with loading state and optional token revocation.

<script lang="ts">
import { SignOutButton } from '@authrim/sveltekit/components';
</script>
<SignOutButton
redirectUri="/goodbye"
revokeTokens={true}
onsuccess={() => console.log('Signed out')}
/>

Props

PropTypeDefaultDescription
redirectUristring | undefinedRedirect after sign out
revokeTokensbooleanfalseRevoke tokens on sign out
disabledbooleanfalseDisabled state
classstring''CSS class

Events

EventPayloadDescription
successvoidSuccessful sign out
errorAuthErrorSign out failed
loadingAuthLoadingStateLoading state changed

UserProfile

Displays the current user’s avatar, name, and email.

<script lang="ts">
import { UserProfile } from '@authrim/sveltekit/components';
</script>
<UserProfile showAvatar={true} showEmail={true} />

Props

PropTypeDefaultDescription
showAvatarbooleantrueShow avatar image
showEmailbooleantrueShow email address
classstring''CSS class

Slots

SlotPropsDescription
Default{ user }Custom layout
avatar{ user }Custom avatar
info{ user }Custom info section
unauthenticatedShown when not authenticated
<UserProfile>
{#snippet avatar({ user })}
<div class="custom-avatar">
{user.displayName?.charAt(0) ?? '?'}
</div>
{/snippet}
{#snippet info({ user })}
<div>
<strong>{user.displayName}</strong>
<span>{user.email}</span>
</div>
{/snippet}
</UserProfile>

Default Styling

  • Avatar: 40×40px, circular, with fallback icon
  • Name: font-weight: 500
  • Email: smaller text, 70% opacity
  • Container: flex row layout

Next Steps