kavach

中文文档
1.3.2发布于 2026/11/04MITTypeScriptES Module构建来源已验证

Drop in security for web apps.

原始使用文档
文档目录7 节
  1. Installation
  2. Quick start
  3. What it does
  4. Configuration
  5. Adapters
  6. API
  7. createKavach(adapter, options?)

kavach

Kavach — Protective Armour. Drop-in security for SvelteKit apps.

Installation

bun add kavach @kavach/adapter-supabase
# pick the adapter for your backend

Quick start

bun add kavach @kavach/vite @kavach/adapter-supabase
# pick the adapter for your backend

Create kavach.config.js in your project root:

export default {
  adapter: 'supabase',
  providers: [
    { name: 'google', label: 'Continue with Google' },
    { name: 'magic', mode: 'otp', label: 'Magic Link' }
  ],
  rules: [
    { path: '/', public: true },
    { path: '/auth', public: true },
    { path: '/dashboard', roles: '*' }
  ],
  env: {
    url: 'PUBLIC_SUPABASE_URL',
    anonKey: 'PUBLIC_SUPABASE_ANON_KEY'
  }
}

Add the Kavach plugin to vite.config.js — it must come before sveltekit():

import { kavach } from '@kavach/vite'
import { sveltekit } from '@sveltejs/kit/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [kavach(), sveltekit()]
})

Register the server hook in src/hooks.server.js:

import { kavach } from '$kavach/auth'
export const handle = kavach.handle
<!-- src/routes/+layout.svelte -->
<script>
  import { setContext, onMount } from 'svelte'
  import { page } from '$app/stores'

  const kavach = $state({})
  setContext('kavach', kavach)

  onMount(async () => {
    const { createKavach } = await import('kavach')
    const { adapter, logger } = await import('$kavach/auth')
    const { invalidateAll } = await import('$app/navigation')
    const instance = createKavach(adapter, { logger, invalidateAll })
    Object.assign(kavach, instance)
    instance.onAuthChange($page.url)
  })
</script>

What it does

  • Route protection — all routes are private by default; define public and role-scoped routes in kavach.config.js
  • Session management — reads and writes session cookies on the server, syncs to the client
  • Auth adapter — delegates sign in, sign out, and token refresh to the backend of your choice
  • Logging — structured JSON logging via @kavach/logger

Configuration

Use kavach init to scaffold configuration, or create kavach.config.js manually:

export default {
  adapter: 'supabase',
  providers: [
    { name: 'google', label: 'Continue with Google' },
    { mode: 'otp', name: 'magic', label: 'Email Magic Link' }
  ],
  routes: {
    auth: '(public)/auth',
    logout: '/logout',
    // per-role landing after login — string or async (session) => path
    home: async (session) => (session.user.role === 'admin' ? '/admin' : '/dashboard')
  },
  rules: [
    { path: '/', public: true },
    { path: '/auth', public: true },
    { path: '/dashboard', roles: '*' }, // any authenticated user
    { path: '/admin', roles: ['admin'] } // only the 'admin' role
  ]
}

Route protection is expressed as a flat rules[] array — public: true for open routes, roles: '*' for any authenticated user, roles: ['role', …] for specific roles. Per-role landing pages are resolved by the routes.home function; per-route redirect overrides use a rule's fallback (a number overrides the status, a string redirects). See Sentry for the full route-rule reference.

Adapters

PackageBackend
@kavach/adapter-supabaseSupabase
@kavach/adapter-firebaseFirebase Auth
@kavach/adapter-auth0Auth0
@kavach/adapter-amplifyAWS Amplify / Cognito
@kavach/adapter-convexConvex

API

createKavach(adapter, options?)

Returns a Kavach instance with:

  • handle — SvelteKit server hook
  • signIn(credentials) — sign in with the configured adapter
  • signOut() — sign out and clear session
  • onAuthChange(url) — handle OAuth callback URLs
  • user — current user (reactive on client)
  • session — current session

版本与发布记录

kavach 共发布 19 个版本,首次发布于 2023/07/14;过去 12 个月发布了 16 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.2latest2026/11/041 天前
1.3.02026/10/2610 天前
1.2.02026/09/121 个月前
1.1.32026/08/282 个月前
1.1.22026/08/282 个月前
1.1.12026/08/282 个月前

其他发布通道:next → 1.0.0-next.35。

关于 kavach 的常见问题

kavach 是什么?

Drop in security for web apps.

如何安装 kavach?

在项目目录运行 npm install kavach,或使用 pnpm add / yarn add / bun add。

kavach 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

kavach 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

kavach 可以商用吗?

kavach 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

kavach 还在维护吗?

最新版本 1.3.2 发布于 2026/11/04(1 天前);至今共发布 19 个版本,其中过去 12 个月发布了 16 个;首次发布于 2023/07/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。