Hooks Setup
The SDK integrates with SvelteKit through the hooks.server.ts file. This is where you configure authentication, storage, and other features.
Basic Setup
Section titled “Basic Setup”Create or update your src/hooks.server.ts:
import { kuratchi } from 'kuratchi-sdk';import { sessionPlugin, adminPlugin, organizationPlugin, credentialsPlugin, oauthPlugin, guardsPlugin, requireAuth} from 'kuratchi-sdk/auth';import { adminSchema } from '$lib/schemas/admin';import { organizationSchema } from '$lib/schemas/organization';import type { Handle } from '@sveltejs/kit';import { env } from '$env/dynamic/private';
export const { handle }: { handle: Handle } = kuratchi({ auth: { plugins: [ sessionPlugin(), adminPlugin({ adminSchema, organizationSchema, adminDatabase: 'ADMIN_DB' }), organizationPlugin({ organizationSchema }), credentialsPlugin(), oauthPlugin({ providers: [ { name: 'google', clientId: env.GOOGLE_CLIENT_ID || '', clientSecret: env.GOOGLE_CLIENT_SECRET || '' }, { name: 'github', clientId: env.GITHUB_CLIENT_ID || '', clientSecret: env.GITHUB_CLIENT_SECRET || '' } ] }), guardsPlugin( requireAuth({ paths: ['*'], exclude: ['/auth/*', '/api/*'], redirectTo: '/auth/signin' }) ) ] }, storage: { kv: { default: 'KV' }, r2: { default: 'BUCKET' } }});Plugin Overview
Section titled “Plugin Overview”sessionPlugin
Section titled “sessionPlugin”Manages user sessions with secure cookies. Always include this plugin first.
adminPlugin
Section titled “adminPlugin”Connects to your admin database for user and organization management.
adminPlugin({ adminSchema, // Your admin DB schema organizationSchema, // Your org DB schema adminDatabase: 'ADMIN_DB'})organizationPlugin
Section titled “organizationPlugin”Enables multi-tenant organization support with per-org databases.
credentialsPlugin
Section titled “credentialsPlugin”Adds email/password authentication.
oauthPlugin
Section titled “oauthPlugin”Configures OAuth providers. Supported providers: google, github.
oauthPlugin({ providers: [ { name: 'google', clientId: env.GOOGLE_CLIENT_ID || '', clientSecret: env.GOOGLE_CLIENT_SECRET || '' } ]})guardsPlugin
Section titled “guardsPlugin”Protects routes based on authentication status.
guardsPlugin( requireAuth({ paths: ['*'], // Protect all routes exclude: ['/auth/*'], // Except auth routes redirectTo: '/auth/signin' }))Storage Bindings
Section titled “Storage Bindings”Configure KV and R2 bindings for your app:
storage: { kv: { default: 'KV', sessions: 'SESSION_KV' }, r2: { default: 'BUCKET', uploads: 'USER_UPLOADS' }}Access in your routes via locals.kuratchi.kv and locals.kuratchi.r2.
What You Get
Section titled “What You Get”After setup, your routes have access to:
locals.user- Current authenticated userlocals.session- Current session datalocals.kuratchi.kv- KV storagelocals.kuratchi.r2- R2 storagelocals.kuratchi.orgDatabaseClient()- Org database client
Example Usage
Section titled “Example Usage”export const load = async ({ locals }) => { return { user: locals.user, session: locals.session };};