Skip to content

Hooks Setup

The SDK integrates with SvelteKit through the hooks.server.ts file. This is where you configure authentication, storage, and other features.

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' }
}
});

Manages user sessions with secure cookies. Always include this plugin first.

Connects to your admin database for user and organization management.

adminPlugin({
adminSchema, // Your admin DB schema
organizationSchema, // Your org DB schema
adminDatabase: 'ADMIN_DB'
})

Enables multi-tenant organization support with per-org databases.

Adds email/password authentication.

Configures OAuth providers. Supported providers: google, github.

oauthPlugin({
providers: [
{
name: 'google',
clientId: env.GOOGLE_CLIENT_ID || '',
clientSecret: env.GOOGLE_CLIENT_SECRET || ''
}
]
})

Protects routes based on authentication status.

guardsPlugin(
requireAuth({
paths: ['*'], // Protect all routes
exclude: ['/auth/*'], // Except auth routes
redirectTo: '/auth/signin'
})
)

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.

After setup, your routes have access to:

  • locals.user - Current authenticated user
  • locals.session - Current session data
  • locals.kuratchi.kv - KV storage
  • locals.kuratchi.r2 - R2 storage
  • locals.kuratchi.orgDatabaseClient() - Org database client
src/routes/+layout.server.ts
export const load = async ({ locals }) => {
return {
user: locals.user,
session: locals.session
};
};