@krutai/auth 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI_REFERENCE.md +26 -12
- package/README.md +13 -10
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
- package/scripts/migrate.js +53 -0
package/AI_REFERENCE.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
## Package Overview
|
|
4
4
|
|
|
5
5
|
- **Name**: `@krutai/auth`
|
|
6
|
-
- **Version**: `0.1.
|
|
6
|
+
- **Version**: `0.1.9`
|
|
7
7
|
- **Purpose**: Authentication package for KrutAI — wraps Better Auth with a `krutAuth` function and optional API key validation via `KrutAuth` class
|
|
8
8
|
- **Entry**: `src/index.ts` → `dist/index.{js,mjs,d.ts}`
|
|
9
9
|
- **Build**: `tsup` (CJS + ESM, all deps external)
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
## Dependency Architecture
|
|
12
12
|
|
|
13
13
|
```
|
|
14
|
-
@krutai/auth@0.1.
|
|
14
|
+
@krutai/auth@0.1.9
|
|
15
15
|
├── dependency: krutai ← API key validation (also peerDep)
|
|
16
16
|
├── dependency: better-auth ← auth engine (external in tsup)
|
|
17
17
|
├── dependency: better-sqlite3 ← default SQLite adapter
|
|
@@ -30,6 +30,8 @@ packages/auth/
|
|
|
30
30
|
│ ├── types.ts # KrutAuthConfig, AuthSession, BetterAuthOptions
|
|
31
31
|
│ ├── react.ts # re-exports better-auth/react (createAuthClient, hooks)
|
|
32
32
|
│ └── next-js.ts # re-exports better-auth/next-js (toNextJsHandler)
|
|
33
|
+
├── scripts/
|
|
34
|
+
│ └── migrate.js # postinstall: auto-migrates SQLite tables via better-auth
|
|
33
35
|
├── package.json
|
|
34
36
|
├── tsconfig.json
|
|
35
37
|
└── tsup.config.ts
|
|
@@ -49,6 +51,8 @@ packages/auth/
|
|
|
49
51
|
|
|
50
52
|
Drop-in replacement for `betterAuth`. Users should always use this.
|
|
51
53
|
|
|
54
|
+
**Always include `baseURL`** to avoid Better Auth base URL warnings:
|
|
55
|
+
|
|
52
56
|
```typescript
|
|
53
57
|
import { krutAuth } from "@krutai/auth";
|
|
54
58
|
import Database from "better-sqlite3";
|
|
@@ -56,11 +60,7 @@ import Database from "better-sqlite3";
|
|
|
56
60
|
export const auth = krutAuth({
|
|
57
61
|
database: new Database("./sqlite.db"),
|
|
58
62
|
emailAndPassword: { enabled: true },
|
|
59
|
-
|
|
60
|
-
additionalFields: {
|
|
61
|
-
name: { type: "string", required: true },
|
|
62
|
-
},
|
|
63
|
-
},
|
|
63
|
+
baseURL: process.env.BETTER_AUTH_BASE_URL ?? "http://localhost:3000",
|
|
64
64
|
});
|
|
65
65
|
```
|
|
66
66
|
|
|
@@ -103,9 +103,12 @@ interface KrutAuthConfig {
|
|
|
103
103
|
|
|
104
104
|
### Validator Re-exports (from `krutai`)
|
|
105
105
|
|
|
106
|
+
> **Note for AI assistants**: Only `validateApiKeyFormat`, `validateApiKeyWithService`, and `ApiKeyValidationError` are exported. `createApiKeyChecker` is **NOT exported**. Do not suggest it.
|
|
107
|
+
> The API key validation is **format/length only** (not null, minimum length). There is no external API check call.
|
|
108
|
+
|
|
106
109
|
```typescript
|
|
107
110
|
// These are re-exported from krutai — NOT defined here
|
|
108
|
-
export { validateApiKeyFormat, validateApiKeyWithService,
|
|
111
|
+
export { validateApiKeyFormat, validateApiKeyWithService, ApiKeyValidationError } from 'krutai';
|
|
109
112
|
```
|
|
110
113
|
|
|
111
114
|
## Usage Examples
|
|
@@ -118,6 +121,7 @@ import Database from "better-sqlite3";
|
|
|
118
121
|
export const auth = krutAuth({
|
|
119
122
|
database: new Database("./sqlite.db"),
|
|
120
123
|
emailAndPassword: { enabled: true },
|
|
124
|
+
baseURL: process.env.BETTER_AUTH_BASE_URL ?? "http://localhost:3000",
|
|
121
125
|
});
|
|
122
126
|
```
|
|
123
127
|
|
|
@@ -165,6 +169,15 @@ try {
|
|
|
165
169
|
}
|
|
166
170
|
```
|
|
167
171
|
|
|
172
|
+
## Auto-Migration (postinstall)
|
|
173
|
+
|
|
174
|
+
`scripts/migrate.js` runs after `npm install` to create all required Better Auth SQLite tables automatically. This means users do NOT need to run any manual migration commands — sign-up and sign-in work out of the box.
|
|
175
|
+
|
|
176
|
+
The script:
|
|
177
|
+
1. Creates `sqlite.db` in the project root (if not present)
|
|
178
|
+
2. Runs `better-auth migrate` to create all required tables
|
|
179
|
+
3. Skips silently if the DB already exists and tables are up to date
|
|
180
|
+
|
|
168
181
|
## tsup Configuration Notes
|
|
169
182
|
|
|
170
183
|
- `better-auth` → **external** (real dependency, NOT bundled)
|
|
@@ -174,10 +187,11 @@ try {
|
|
|
174
187
|
|
|
175
188
|
## Important Notes
|
|
176
189
|
|
|
177
|
-
1. **
|
|
178
|
-
2. **
|
|
179
|
-
3. **
|
|
180
|
-
4.
|
|
190
|
+
1. **Always set `baseURL`**: Pass `baseURL: process.env.BETTER_AUTH_BASE_URL` in `krutAuth()` options
|
|
191
|
+
2. **Use `krutAuth` not `betterAuth`**: The public API is `krutAuth`. `betterAuth` is an internal implementation detail
|
|
192
|
+
3. **Validator lives in `krutai`**: Never add a local `validator.ts` — import from `krutai`
|
|
193
|
+
4. **No `noExternal` for `better-auth` or `krutai`**: They must stay external in tsup
|
|
194
|
+
5. **`getBetterAuth()` returns `Auth`**: Uses the `Auth` type from `better-auth`, not `ReturnType<typeof betterAuth>`
|
|
181
195
|
|
|
182
196
|
## Related Packages
|
|
183
197
|
|
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ Authentication package for KrutAI powered by [Better Auth](https://www.better-au
|
|
|
7
7
|
- 🔐 **API Key Protection** — Requires a valid KrutAI API key (validated via `krutai`)
|
|
8
8
|
- 🚀 **Better Auth Integration** — Built on top of Better Auth
|
|
9
9
|
- 📦 **Auto-installs everything** — `krutai`, `better-auth`, `better-sqlite3` install automatically
|
|
10
|
+
- 🗄️ **Auto-migrates SQLite** — Database tables are created automatically on install
|
|
10
11
|
- 🎯 **Next.js Ready** — First-class support via `@krutai/auth/next-js`
|
|
11
12
|
- ⚡ **Dual Format** — Supports both ESM and CommonJS
|
|
12
13
|
- 🔷 **TypeScript First** — Full type safety and IntelliSense
|
|
@@ -17,7 +18,7 @@ Authentication package for KrutAI powered by [Better Auth](https://www.better-au
|
|
|
17
18
|
npm install @krutai/auth
|
|
18
19
|
```
|
|
19
20
|
|
|
20
|
-
> **Note:** `krutai`, `better-auth`, `better-sqlite3`, and `@types/better-sqlite3` are all installed automatically.
|
|
21
|
+
> **Note:** `krutai`, `better-auth`, `better-sqlite3`, and `@types/better-sqlite3` are all installed automatically. SQLite tables are migrated automatically after install.
|
|
21
22
|
|
|
22
23
|
## Quick Start
|
|
23
24
|
|
|
@@ -33,17 +34,12 @@ export const auth = krutAuth({
|
|
|
33
34
|
emailAndPassword: {
|
|
34
35
|
enabled: true,
|
|
35
36
|
},
|
|
36
|
-
|
|
37
|
-
additionalFields: {
|
|
38
|
-
name: {
|
|
39
|
-
type: "string",
|
|
40
|
-
required: true,
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
},
|
|
37
|
+
baseURL: process.env.BETTER_AUTH_BASE_URL ?? "http://localhost:3000",
|
|
44
38
|
});
|
|
45
39
|
```
|
|
46
40
|
|
|
41
|
+
> **Required:** Set `BETTER_AUTH_BASE_URL` in your `.env` file (e.g. `http://localhost:3000` for dev, `https://yourdomain.com` for production). Without this, redirects and callbacks will not work.
|
|
42
|
+
|
|
47
43
|
### API Route handler
|
|
48
44
|
|
|
49
45
|
```typescript
|
|
@@ -82,6 +78,13 @@ const auth = new KrutAuth({
|
|
|
82
78
|
await auth.initialize();
|
|
83
79
|
```
|
|
84
80
|
|
|
81
|
+
## Environment Variables
|
|
82
|
+
|
|
83
|
+
| Variable | Required | Description |
|
|
84
|
+
|---|---|---|
|
|
85
|
+
| `BETTER_AUTH_BASE_URL` | ✅ | Your app's base URL (e.g. `http://localhost:3000`) |
|
|
86
|
+
| `BETTER_AUTH_SECRET` | recommended | Secret for signing sessions |
|
|
87
|
+
|
|
85
88
|
## Exports
|
|
86
89
|
|
|
87
90
|
| Import path | What it provides |
|
|
@@ -137,7 +140,7 @@ try {
|
|
|
137
140
|
## Architecture
|
|
138
141
|
|
|
139
142
|
```
|
|
140
|
-
@krutai/auth@0.1.
|
|
143
|
+
@krutai/auth@0.1.9
|
|
141
144
|
├── dependency: krutai ← API key validation
|
|
142
145
|
├── dependency: better-auth ← auth engine
|
|
143
146
|
├── dependency: better-sqlite3 ← default database adapter
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as better_auth from 'better-auth';
|
|
2
2
|
import { BetterAuthOptions, Auth } from 'better-auth';
|
|
3
3
|
export { BetterAuthOptions } from 'better-auth';
|
|
4
|
-
export { ApiKeyValidationError,
|
|
4
|
+
export { ApiKeyValidationError, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Configuration options for KrutAuth
|
|
@@ -140,6 +140,6 @@ declare class KrutAuth {
|
|
|
140
140
|
*/
|
|
141
141
|
declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
|
|
142
142
|
|
|
143
|
-
declare const VERSION = "0.1.
|
|
143
|
+
declare const VERSION = "0.1.9";
|
|
144
144
|
|
|
145
145
|
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as better_auth from 'better-auth';
|
|
2
2
|
import { BetterAuthOptions, Auth } from 'better-auth';
|
|
3
3
|
export { BetterAuthOptions } from 'better-auth';
|
|
4
|
-
export { ApiKeyValidationError,
|
|
4
|
+
export { ApiKeyValidationError, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Configuration options for KrutAuth
|
|
@@ -140,6 +140,6 @@ declare class KrutAuth {
|
|
|
140
140
|
*/
|
|
141
141
|
declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
|
|
142
142
|
|
|
143
|
-
declare const VERSION = "0.1.
|
|
143
|
+
declare const VERSION = "0.1.9";
|
|
144
144
|
|
|
145
145
|
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
|
package/dist/index.js
CHANGED
|
@@ -96,16 +96,12 @@ var KrutAuth = class {
|
|
|
96
96
|
function krutAuth(options) {
|
|
97
97
|
return betterAuth.betterAuth(options);
|
|
98
98
|
}
|
|
99
|
-
var VERSION = "0.1.
|
|
99
|
+
var VERSION = "0.1.9";
|
|
100
100
|
|
|
101
101
|
Object.defineProperty(exports, "ApiKeyValidationError", {
|
|
102
102
|
enumerable: true,
|
|
103
103
|
get: function () { return krutai.ApiKeyValidationError; }
|
|
104
104
|
});
|
|
105
|
-
Object.defineProperty(exports, "createApiKeyChecker", {
|
|
106
|
-
enumerable: true,
|
|
107
|
-
get: function () { return krutai.createApiKeyChecker; }
|
|
108
|
-
});
|
|
109
105
|
Object.defineProperty(exports, "validateApiKeyFormat", {
|
|
110
106
|
enumerable: true,
|
|
111
107
|
get: function () { return krutai.validateApiKeyFormat; }
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/index.ts"],"names":["validateApiKeyFormat","validateApiKeyWithService","betterAuth"],"mappings":";;;;;;AAiCO,IAAM,WAAN,MAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUlB,YAAoB,MAAA,EAAwB;AAAxB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEhB,IAAAA,2BAAA,CAAqB,OAAO,MAAM,CAAA;AAClC,IAAA,IAAA,CAAK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AAA,CAAO;AAAA,KAClB,CAAA;AAAA,EACL;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,aAAA,GAAsB;AAClB,IAAA,IAAI,CAAC,KAAK,kBAAA,EAAoB;AAC1B,MAAA,MAAM,IAAI,KAAA;AAAA,QACN;AAAA,OACJ;AAAA,IACJ;AACA,IAAA,OAAO,IAAA,CAAK,kBAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA,EAKA,aAAA,GAAyB;AACrB,IAAA,OAAO,IAAA,CAAK,WAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAA,GAAoB;AAChB,IAAA,OAAO,IAAA,CAAK,MAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,MAAA,GAAwB;AAC1B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAA,GAAyB;AAC3B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAA,GAA4B;AAC9B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AACJ;ACrFO,SAAS,SAAS,OAAA,EAA4B;AACjD,EAAA,OAAOA,sBAAW,OAAO,CAAA;AAC7B;AAgBO,IAAM,OAAA,GAAU","file":"index.js","sourcesContent":["import { betterAuth } from 'better-auth';\nimport type { Auth } from 'better-auth';\nimport type { KrutAuthConfig } from './types';\n// Import validation from krutai peer dependency\nimport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n} from 'krutai';\n\n/**\n * KrutAuth - Authentication client for KrutAI\n *\n * This class wraps Better Auth and adds API key validation\n * to ensure only authorized users can access authentication features.\n *\n * @example\n * ```typescript\n * import { KrutAuth } from '@krutai/auth';\n *\n * const auth = new KrutAuth({\n * apiKey: 'your-api-key-here',\n * betterAuthOptions: {\n * // Better Auth configuration\n * }\n * });\n *\n * // Initialize the client (validates API key)\n * await auth.initialize();\n *\n * // Use authentication features\n * const betterAuth = auth.getBetterAuth();\n * ```\n */\nexport class KrutAuth {\n private apiKey: string;\n private betterAuthInstance: Auth | null = null;\n private initialized = false;\n\n /**\n * Creates a new KrutAuth instance\n * @param config - Configuration options\n * @throws {ApiKeyValidationError} If API key is invalid\n */\n constructor(private config: KrutAuthConfig) {\n // Validate API key format immediately\n validateApiKeyFormat(config.apiKey);\n this.apiKey = config.apiKey;\n\n // Initialize if validation is not required on init\n if (config.validateOnInit === false) {\n this.initializeBetterAuth();\n }\n }\n\n /**\n * Initialize the authentication client\n * Validates the API key and sets up Better Auth\n * @throws {ApiKeyValidationError} If API key validation fails\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n // Validate API key with service if needed\n if (this.config.validateOnInit !== false) {\n await validateApiKeyWithService(this.apiKey);\n }\n\n this.initializeBetterAuth();\n this.initialized = true;\n }\n\n /**\n * Initialize Better Auth instance\n * @private\n */\n private initializeBetterAuth(): void {\n this.betterAuthInstance = betterAuth({\n ...this.config.betterAuthOptions,\n });\n }\n\n /**\n * Get the Better Auth instance\n * @throws {Error} If not initialized\n */\n getBetterAuth(): Auth {\n if (!this.betterAuthInstance) {\n throw new Error(\n 'KrutAuth not initialized. Call initialize() first or set validateOnInit to false.'\n );\n }\n return this.betterAuthInstance;\n }\n\n /**\n * Check if the client is initialized\n */\n isInitialized(): boolean {\n return this.initialized;\n }\n\n /**\n * Get the API key (useful for making authenticated requests)\n * @returns The API key\n */\n getApiKey(): string {\n return this.apiKey;\n }\n\n /**\n * Sign in a user\n * This is a convenience method that wraps Better Auth\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signIn(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Sign out the current user\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signOut(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Get the current session\n * You can access the full Better Auth API via getBetterAuth()\n */\n async getSession(): Promise<Auth> {\n return this.getBetterAuth();\n }\n}\n","/**\n * @krutai/auth - Authentication package for KrutAI\n *\n * Requires `krutai` as a peer dependency (installed automatically).\n *\n * @example Server-side (Next.js API route / server component)\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * baseURL: process.env.BETTER_AUTH_BASE_URL ?? \"http://localhost:3000\",\n * });\n * ```\n *\n * @example Client-side (React / Next.js client component)\n * ```typescript\n * import { createAuthClient } from \"@krutai/auth/react\";\n *\n * export const authClient = createAuthClient({\n * baseURL: process.env.NEXT_PUBLIC_APP_URL ?? \"http://localhost:3000\",\n * });\n *\n * export const { signIn, signUp, signOut, useSession } = authClient;\n * ```\n *\n * @packageDocumentation\n */\n\nimport { betterAuth } from 'better-auth';\nimport type { BetterAuthOptions } from 'better-auth';\n\n/**\n * krutAuth — drop-in replacement for betterAuth.\n *\n * Use this instead of importing betterAuth directly.\n *\n * @example\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * });\n * ```\n */\nexport function krutAuth(options: BetterAuthOptions) {\n return betterAuth(options);\n}\n\n// Export main KrutAuth class (API-key-protected wrapper)\nexport { KrutAuth } from './client';\n\n// Export types\nexport type { KrutAuthConfig, AuthSession, BetterAuthOptions } from './types';\n\n// Re-export minimal validator utilities (format/length check only)\nexport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n ApiKeyValidationError,\n} from 'krutai';\n\n// Package metadata\nexport const VERSION = '0.1.9';\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { betterAuth } from 'better-auth';
|
|
2
2
|
import { validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
3
|
-
export { ApiKeyValidationError,
|
|
3
|
+
export { ApiKeyValidationError, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
4
4
|
|
|
5
5
|
// src/index.ts
|
|
6
6
|
var KrutAuth = class {
|
|
@@ -95,7 +95,7 @@ var KrutAuth = class {
|
|
|
95
95
|
function krutAuth(options) {
|
|
96
96
|
return betterAuth(options);
|
|
97
97
|
}
|
|
98
|
-
var VERSION = "0.1.
|
|
98
|
+
var VERSION = "0.1.9";
|
|
99
99
|
|
|
100
100
|
export { KrutAuth, VERSION, krutAuth };
|
|
101
101
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/index.ts"],"names":["betterAuth"],"mappings":";;;;;AAiCO,IAAM,WAAN,MAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUlB,YAAoB,MAAA,EAAwB;AAAxB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEhB,IAAA,oBAAA,CAAqB,OAAO,MAAM,CAAA;AAClC,IAAA,IAAA,CAAK,SAAS,MAAA,CAAO,MAAA;AAGrB,IAAA,IAAI,MAAA,CAAO,mBAAmB,KAAA,EAAO;AACjC,MAAA,IAAA,CAAK,oBAAA,EAAqB;AAAA,IAC9B;AAAA,EACJ;AAAA,EAlBQ,MAAA;AAAA,EACA,kBAAA,GAAkC,IAAA;AAAA,EAClC,WAAA,GAAc,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBtB,MAAM,UAAA,GAA4B;AAC9B,IAAA,IAAI,KAAK,WAAA,EAAa;AAClB,MAAA;AAAA,IACJ;AAGA,IAAA,IAAI,IAAA,CAAK,MAAA,CAAO,cAAA,KAAmB,KAAA,EAAO;AACtC,MAAA,MAAM,yBAAA,CAA0B,KAAK,MAAM,CAAA;AAAA,IAC/C;AAEA,IAAA,IAAA,CAAK,oBAAA,EAAqB;AAC1B,IAAA,IA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AA;AAAA,EAMA,MAAM,OAAA,GAAyB;AAC3B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAA,GAA4B;AAC9B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AACJ;ACrFO,SAAS,SAAS,OAAA,EAA4B;AACjD,EAAA,OAAOA,WAAW,OAAO,CAAA;AAC7B;AAgBO,IAAM,OAAA,GAAU","file":"index.mjs","sourcesContent":["import { betterAuth } from 'better-auth';\nimport type { Auth } from 'better-auth';\nimport type { KrutAuthConfig } from './types';\n// Import validation from krutai peer dependency\nimport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n} from 'krutai';\n\n/**\n * KrutAuth - Authentication client for KrutAI\n *\n * This class wraps Better Auth and adds API key validation\n * to ensure only authorized users can access authentication features.\n *\n * @example\n * ```typescript\n * import { KrutAuth } from '@krutai/auth';\n *\n * const auth = new KrutAuth({\n * apiKey: 'your-api-key-here',\n * betterAuthOptions: {\n * // Better Auth configuration\n * }\n * });\n *\n * // Initialize the client (validates API key)\n * await auth.initialize();\n *\n * // Use authentication features\n * const betterAuth = auth.getBetterAuth();\n * ```\n */\nexport class KrutAuth {\n private apiKey: string;\n private betterAuthInstance: Auth | null = null;\n private initialized = false;\n\n /**\n * Creates a new KrutAuth instance\n * @param config - Configuration options\n * @throws {ApiKeyValidationError} If API key is invalid\n */\n constructor(private config: KrutAuthConfig) {\n // Validate API key format immediately\n validateApiKeyFormat(config.apiKey);\n this.apiKey = config.apiKey;\n\n // Initialize if validation is not required on init\n if (config.validateOnInit === false) {\n this.initializeBetterAuth();\n }\n }\n\n /**\n * Initialize the authentication client\n * Validates the API key and sets up Better Auth\n * @throws {ApiKeyValidationError} If API key validation fails\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n // Validate API key with service if needed\n if (this.config.validateOnInit !== false) {\n await validateApiKeyWithService(this.apiKey);\n }\n\n this.initializeBetterAuth();\n this.initialized = true;\n }\n\n /**\n * Initialize Better Auth instance\n * @private\n */\n private initializeBetterAuth(): void {\n this.betterAuthInstance = betterAuth({\n ...this.config.betterAuthOptions,\n });\n }\n\n /**\n * Get the Better Auth instance\n * @throws {Error} If not initialized\n */\n getBetterAuth(): Auth {\n if (!this.betterAuthInstance) {\n throw new Error(\n 'KrutAuth not initialized. Call initialize() first or set validateOnInit to false.'\n );\n }\n return this.betterAuthInstance;\n }\n\n /**\n * Check if the client is initialized\n */\n isInitialized(): boolean {\n return this.initialized;\n }\n\n /**\n * Get the API key (useful for making authenticated requests)\n * @returns The API key\n */\n getApiKey(): string {\n return this.apiKey;\n }\n\n /**\n * Sign in a user\n * This is a convenience method that wraps Better Auth\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signIn(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Sign out the current user\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signOut(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Get the current session\n * You can access the full Better Auth API via getBetterAuth()\n */\n async getSession(): Promise<Auth> {\n return this.getBetterAuth();\n }\n}\n","/**\n * @krutai/auth - Authentication package for KrutAI\n *\n * Requires `krutai` as a peer dependency (installed automatically).\n *\n * @example Server-side (Next.js API route / server component)\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * baseURL: process.env.BETTER_AUTH_BASE_URL ?? \"http://localhost:3000\",\n * });\n * ```\n *\n * @example Client-side (React / Next.js client component)\n * ```typescript\n * import { createAuthClient } from \"@krutai/auth/react\";\n *\n * export const authClient = createAuthClient({\n * baseURL: process.env.NEXT_PUBLIC_APP_URL ?? \"http://localhost:3000\",\n * });\n *\n * export const { signIn, signUp, signOut, useSession } = authClient;\n * ```\n *\n * @packageDocumentation\n */\n\nimport { betterAuth } from 'better-auth';\nimport type { BetterAuthOptions } from 'better-auth';\n\n/**\n * krutAuth — drop-in replacement for betterAuth.\n *\n * Use this instead of importing betterAuth directly.\n *\n * @example\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * });\n * ```\n */\nexport function krutAuth(options: BetterAuthOptions) {\n return betterAuth(options);\n}\n\n// Export main KrutAuth class (API-key-protected wrapper)\nexport { KrutAuth } from './client';\n\n// Export types\nexport type { KrutAuthConfig, AuthSession, BetterAuthOptions } from './types';\n\n// Re-export minimal validator utilities (format/length check only)\nexport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n ApiKeyValidationError,\n} from 'krutai';\n\n// Package metadata\nexport const VERSION = '0.1.9';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krutai/auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Authentication package for KrutAI — installs krutai, better-auth, and better-sqlite3 automatically",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"dist",
|
|
27
|
+
"scripts",
|
|
27
28
|
"README.md",
|
|
28
29
|
"AI_REFERENCE.md"
|
|
29
30
|
],
|
|
@@ -31,7 +32,8 @@
|
|
|
31
32
|
"build": "tsup",
|
|
32
33
|
"dev": "tsup --watch",
|
|
33
34
|
"clean": "rm -rf dist *.tsbuildinfo",
|
|
34
|
-
"typecheck": "tsc --noEmit"
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"postinstall": "node scripts/migrate.js"
|
|
35
37
|
},
|
|
36
38
|
"keywords": [
|
|
37
39
|
"krutai",
|
|
@@ -63,4 +65,4 @@
|
|
|
63
65
|
"url": "git+https://github.com/AccountantAIOrg/krut_packages.git",
|
|
64
66
|
"directory": "packages/auth"
|
|
65
67
|
}
|
|
66
|
-
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @krutai/auth — postinstall migration script
|
|
4
|
+
*
|
|
5
|
+
* Automatically creates (or updates) the SQLite database tables
|
|
6
|
+
* required by Better Auth. Runs after `npm install @krutai/auth`.
|
|
7
|
+
*
|
|
8
|
+
* The database file is placed at the project root as `sqlite.db`.
|
|
9
|
+
* Set BETTER_AUTH_DB_PATH to override the location.
|
|
10
|
+
* Set SKIP_KRUTAI_MIGRATE=1 to skip this script entirely.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { execSync } = require('child_process');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
|
|
17
|
+
// Allow opt-out
|
|
18
|
+
if (process.env.SKIP_KRUTAI_MIGRATE === '1') {
|
|
19
|
+
console.log('[krutai/auth] Skipping migration (SKIP_KRUTAI_MIGRATE=1)');
|
|
20
|
+
process.exit(0);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Determine project root (where the consuming project's package.json lives)
|
|
24
|
+
// When postinstall runs under node_modules/@krutai/auth, we go up 3 levels.
|
|
25
|
+
const projectRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
26
|
+
|
|
27
|
+
// Fallback: if we're being run from within the monorepo itself, use cwd
|
|
28
|
+
const packageJson = path.join(projectRoot, 'package.json');
|
|
29
|
+
const root = fs.existsSync(packageJson) ? projectRoot : process.cwd();
|
|
30
|
+
|
|
31
|
+
const dbPath = process.env.BETTER_AUTH_DB_PATH ?? path.join(root, 'sqlite.db');
|
|
32
|
+
|
|
33
|
+
console.log('[krutai/auth] Running Better Auth SQLite migration...');
|
|
34
|
+
console.log(`[krutai/auth] Database: ${dbPath}`);
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
// better-auth provides a CLI via `npx better-auth migrate`
|
|
38
|
+
// Pass the db path via env so it's picked up automatically
|
|
39
|
+
execSync('npx better-auth migrate --yes', {
|
|
40
|
+
cwd: root,
|
|
41
|
+
stdio: 'inherit',
|
|
42
|
+
env: {
|
|
43
|
+
...process.env,
|
|
44
|
+
BETTER_AUTH_DB_PATH: dbPath,
|
|
45
|
+
BETTER_AUTH_DATABASE_URL: `file:${dbPath}`,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
console.log('[krutai/auth] Migration complete. SQLite tables are ready.');
|
|
49
|
+
} catch (err) {
|
|
50
|
+
// Don't fail the install — the user may not have a lib/auth.ts yet
|
|
51
|
+
console.warn('[krutai/auth] Migration skipped or failed (this is OK if you have not created lib/auth.ts yet).');
|
|
52
|
+
console.warn('[krutai/auth] Run `npx better-auth migrate` manually after setting up your auth config.');
|
|
53
|
+
}
|