概要
@authrim/sveltekit/components は認証パターンのための5つのコアコンポーネントを提供します:
| コンポーネント | 用途 |
|---|
AuthProvider | コンテキストプロバイダー — 認証インスタンスをアプリに提供 |
ProtectedRoute | ルートガード — 未認証ユーザーをリダイレクト |
SignInButton | Passkeyまたはソーシャルサインイン(状態管理込み) |
SignOutButton | ローディング状態付きサインアウト |
UserProfile | 現在のユーザー情報を表示 |
AuthProvider
アプリをラップし、Svelteコンテキスト経由で認証クライアントを提供します。SSRセッション同期も処理します。
import { AuthProvider } from '@authrim/sveltekit/components';
import type { AuthrimClient } from '@authrim/sveltekit';
let { data, children } = $props();
let auth: AuthrimClient | null = $state(null);
initialSession={data.auth?.session ?? null}
initialUser={data.auth?.user ?? null}
Props
| Prop | 型 | デフォルト | 説明 |
|---|
auth | AuthrimClient | 必須 | Authrimクライアントインスタンス |
initialSession | Session | null | null | ハイドレーション用SSRセッション |
initialUser | User | null | null | ハイドレーション用SSRユーザー |
動作
setAuthContext(auth) でコンテキストを設定 — すべての子コンポーネントで getAuthContext() が利用可能に
auth._syncFromSSR() を同期的に呼び出し、最初のレンダリング前にストアを設定
- マウント時にバックグラウンドでセッションを再検証(オプション)
- 破棄時に
auth.destroy() を呼び出してイベントリスナーとタイマーをクリーンアップ
コンテキストAPI
AuthProvider の子コンポーネント内で:
import { getAuthContext } from '@authrim/sveltekit';
const auth = getAuthContext();
// auth.passkey, auth.stores, auth.on() 等にアクセス
ProtectedRoute
ユーザーが認証済みの場合のみコンテンツを表示。オプションでログインページにリダイレクト。
import { ProtectedRoute } from '@authrim/sveltekit/components';
<ProtectedRoute redirectTo="/login">
Props
| Prop | 型 | デフォルト | 説明 |
|---|
redirectTo | string | undefined | — | 未認証ユーザーのリダイレクト先 |
includeReturnPath | boolean | true | 現在のパスをクエリパラメータに追加 |
returnPathParam | string | 'redirectTo' | クエリパラメータ名 |
class | string | '' | CSSクラス |
スロット
| スロット | 表示条件 | Props |
|---|
| デフォルト | ユーザーが認証済み | — |
loading | セッション確認中 | — |
unauthenticated | 未認証で redirectTo なし | — |
<ProtectedRoute redirectTo="/login">
<div class="spinner">セッション確認中...</div>
{#snippet unauthenticated()}
ローディング状態を内蔵したPasskeyまたはソーシャルサインインボタン。
import { SignInButton } from '@authrim/sveltekit/components';
import { goto } from '$app/navigation';
onsuccess={() => goto('/')}
onerror={(e) => console.error(e.detail.message)}
onsuccess={() => goto('/')}
Props
| Prop | 型 | デフォルト | 説明 |
|---|
method | 'passkey' | 'social' | 'passkey' | 認証方式 |
provider | SocialProvider | — | method='social' 時に必須 |
disabled | boolean | false | 無効状態 |
class | string | '' | CSSクラス |
イベント
| イベント | ペイロード | 説明 |
|---|
success | { session: Session; user: User } | サインイン成功 |
error | AuthError | サインイン失敗 |
loading | AuthLoadingState | ローディング状態変更 |
カスタムコンテンツ
<SignInButton method="passkey">
ローディング状態とオプションのトークン失効付きサインアウトボタン。
import { SignOutButton } from '@authrim/sveltekit/components';
onsuccess={() => console.log('サインアウト完了')}
Props
| Prop | 型 | デフォルト | 説明 |
|---|
redirectUri | string | undefined | — | サインアウト後のリダイレクト先 |
revokeTokens | boolean | false | サインアウト時にトークンを失効 |
disabled | boolean | false | 無効状態 |
class | string | '' | CSSクラス |
イベント
| イベント | ペイロード | 説明 |
|---|
success | void | サインアウト成功 |
error | AuthError | サインアウト失敗 |
loading | AuthLoadingState | ローディング状態変更 |
UserProfile
現在のユーザーのアバター、名前、メールアドレスを表示。
import { UserProfile } from '@authrim/sveltekit/components';
<UserProfile showAvatar={true} showEmail={true} />
Props
| Prop | 型 | デフォルト | 説明 |
|---|
showAvatar | boolean | true | アバター画像を表示 |
showEmail | boolean | true | メールアドレスを表示 |
class | string | '' | CSSクラス |
スロット
| スロット | Props | 説明 |
|---|
| デフォルト | { user } | カスタムレイアウト |
avatar | { user } | カスタムアバター |
info | { user } | カスタム情報セクション |
unauthenticated | — | 未認証時に表示 |
{#snippet avatar({ user })}
<div class="custom-avatar">
{user.displayName?.charAt(0) ?? '?'}
{#snippet info({ user })}
<strong>{user.displayName}</strong>
<span>{user.email}</span>
デフォルトスタイリング
- アバター: 40×40px、円形、フォールバックアイコン付き
- 名前:
font-weight: 500
- メール: 小さいテキスト、不透明度70%
- コンテナ: flex row レイアウト
次のステップ