Skip to content

Components

@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

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}
PropTypeDefaultDescription
authAuthrimClientrequiredAuthrim client instance
initialSessionSession | nullnullSSR session for hydration
initialUserUser | nullnullSSR user for hydration
  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 fetches the session when _shouldFetchSessionOnMount() is true
  4. On destroy, calls auth.destroy() to clean up event listeners and timers

Inside any child component of AuthProvider:

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

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>
PropTypeDefaultDescription
redirectTostring | undefinedRedirect URL for unauthenticated users
includeReturnPathbooleantrueAppend current path as query param
returnPathParamstring'redirectTo'Query parameter name
classstring''CSS class
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>

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('/')}
/>
PropTypeDefaultDescription
method'passkey' | 'social''passkey'Authentication method
providerSocialProviderRequired when method='social'
disabledbooleanfalseDisabled state
classstring''CSS class
EventPayloadDescription
success{ session: Session; user: User }Successful sign-in
errorAuthErrorSign-in failed
loadingAuthLoadingStateLoading state changed
<SignInButton method="passkey">
🔑 Sign in with your passkey
</SignInButton>

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')}
/>
PropTypeDefaultDescription
redirectUristring | undefinedRedirect after sign out
revokeTokensbooleanfalseRevoke tokens on sign out
disabledbooleanfalseDisabled state
classstring''CSS class
EventPayloadDescription
successvoidSuccessful sign out
errorAuthErrorSign out failed
loadingAuthLoadingStateLoading state changed

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

<script lang="ts">
import { UserProfile } from '@authrim/sveltekit/components';
</script>
<UserProfile showAvatar={true} showEmail={true} />
PropTypeDefaultDescription
showAvatarbooleantrueShow avatar image
showEmailbooleantrueShow email address
classstring''CSS class
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.name ?? user.email)?.charAt(0) ?? '?'}
</div>
{/snippet}
{#snippet info({ user })}
<div>
<strong>{user.name ?? user.email}</strong>
<span>{user.email}</span>
</div>
{/snippet}
</UserProfile>
  • Avatar: 40×40px, circular, with fallback icon
  • Name: font-weight: 500
  • Email: smaller text, 70% opacity
  • Container: flex row layout