@krutai/auth 0.1.6 → 0.1.8
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 +94 -82
- package/README.md +38 -19
- package/dist/index.d.mts +12 -23
- package/dist/index.d.ts +12 -23
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
- package/scripts/migrate.js +53 -0
package/AI_REFERENCE.md
CHANGED
|
@@ -3,32 +3,35 @@
|
|
|
3
3
|
## Package Overview
|
|
4
4
|
|
|
5
5
|
- **Name**: `@krutai/auth`
|
|
6
|
-
- **Version**: `0.1.
|
|
7
|
-
- **Purpose**: Authentication package for KrutAI — wraps Better Auth with
|
|
6
|
+
- **Version**: `0.1.8`
|
|
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
|
-
- **Build**: `tsup` (CJS + ESM,
|
|
9
|
+
- **Build**: `tsup` (CJS + ESM, all deps external)
|
|
10
10
|
|
|
11
11
|
## Dependency Architecture
|
|
12
12
|
|
|
13
13
|
```
|
|
14
|
-
@krutai/auth@0.1.
|
|
15
|
-
├──
|
|
16
|
-
├── dependency:
|
|
17
|
-
|
|
14
|
+
@krutai/auth@0.1.8
|
|
15
|
+
├── dependency: krutai ← API key validation (also peerDep)
|
|
16
|
+
├── dependency: better-auth ← auth engine (external in tsup)
|
|
17
|
+
├── dependency: better-sqlite3 ← default SQLite adapter
|
|
18
|
+
└── dependency: @types/better-sqlite3
|
|
18
19
|
```
|
|
19
20
|
|
|
20
|
-
> **Important for AI**:
|
|
21
|
+
> **Important for AI**: Do NOT bundle `better-auth` or `krutai` inline (no `noExternal`). They are real dependencies and must stay external in tsup.
|
|
21
22
|
|
|
22
23
|
## File Structure
|
|
23
24
|
|
|
24
25
|
```
|
|
25
26
|
packages/auth/
|
|
26
27
|
├── src/
|
|
27
|
-
│ ├── index.ts #
|
|
28
|
-
│ ├── client.ts # KrutAuth class
|
|
28
|
+
│ ├── index.ts # Exports krutAuth function + KrutAuth class + validator re-exports
|
|
29
|
+
│ ├── client.ts # KrutAuth class (API-key-protected wrapper)
|
|
29
30
|
│ ├── types.ts # KrutAuthConfig, AuthSession, BetterAuthOptions
|
|
30
|
-
│ ├── react.ts #
|
|
31
|
-
│ └── next-js.ts #
|
|
31
|
+
│ ├── react.ts # re-exports better-auth/react (createAuthClient, hooks)
|
|
32
|
+
│ └── next-js.ts # re-exports better-auth/next-js (toNextJsHandler)
|
|
33
|
+
├── scripts/
|
|
34
|
+
│ └── migrate.js # postinstall: auto-migrates SQLite tables via better-auth
|
|
32
35
|
├── package.json
|
|
33
36
|
├── tsconfig.json
|
|
34
37
|
└── tsup.config.ts
|
|
@@ -38,28 +41,53 @@ packages/auth/
|
|
|
38
41
|
|
|
39
42
|
| Import | File | Purpose |
|
|
40
43
|
|---|---|---|
|
|
41
|
-
| `@krutai/auth` | `dist/index.js` |
|
|
42
|
-
| `@krutai/auth/react` | `dist/react.js` |
|
|
43
|
-
| `@krutai/auth/next-js` | `dist/next-js.js` |
|
|
44
|
+
| `@krutai/auth` | `dist/index.js` | `krutAuth`, `KrutAuth`, validator re-exports |
|
|
45
|
+
| `@krutai/auth/react` | `dist/react.js` | `createAuthClient`, hooks |
|
|
46
|
+
| `@krutai/auth/next-js` | `dist/next-js.js` | `toNextJsHandler` |
|
|
44
47
|
|
|
45
48
|
## Main Exports
|
|
46
49
|
|
|
47
|
-
###
|
|
50
|
+
### `krutAuth(options)` ← PRIMARY API
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
Drop-in replacement for `betterAuth`. Users should always use this.
|
|
53
|
+
|
|
54
|
+
**Always include `baseURL`** to avoid Better Auth base URL warnings:
|
|
51
55
|
|
|
52
|
-
**Constructor:**
|
|
53
56
|
```typescript
|
|
54
|
-
|
|
57
|
+
import { krutAuth } from "@krutai/auth";
|
|
58
|
+
import Database from "better-sqlite3";
|
|
59
|
+
|
|
60
|
+
export const auth = krutAuth({
|
|
61
|
+
database: new Database("./sqlite.db"),
|
|
62
|
+
emailAndPassword: { enabled: true },
|
|
63
|
+
baseURL: process.env.BETTER_AUTH_BASE_URL ?? "http://localhost:3000",
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `KrutAuth` class ← API-KEY-PROTECTED WRAPPER
|
|
68
|
+
|
|
69
|
+
For when you need API key validation before initializing auth.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { KrutAuth } from "@krutai/auth";
|
|
73
|
+
|
|
74
|
+
const auth = new KrutAuth({
|
|
75
|
+
apiKey: process.env.KRUTAI_API_KEY!,
|
|
76
|
+
betterAuthOptions: {
|
|
77
|
+
database: { provider: 'postgres', url: process.env.DATABASE_URL },
|
|
78
|
+
emailAndPassword: { enabled: true },
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
await auth.initialize();
|
|
83
|
+
const betterAuthInstance = auth.getBetterAuth();
|
|
55
84
|
```
|
|
56
85
|
|
|
57
86
|
**Methods:**
|
|
58
87
|
- `initialize(): Promise<void>` — validates API key + initializes Better Auth
|
|
59
|
-
- `getBetterAuth()` — returns the Better Auth instance
|
|
88
|
+
- `getBetterAuth(): Auth` — returns the Better Auth `Auth` instance
|
|
60
89
|
- `isInitialized(): boolean`
|
|
61
90
|
- `getApiKey(): string`
|
|
62
|
-
- `signIn()`, `signOut()`, `getSession()` — convenience wrappers
|
|
63
91
|
|
|
64
92
|
### Types
|
|
65
93
|
|
|
@@ -73,14 +101,6 @@ interface KrutAuthConfig {
|
|
|
73
101
|
}
|
|
74
102
|
```
|
|
75
103
|
|
|
76
|
-
#### `AuthSession`
|
|
77
|
-
```typescript
|
|
78
|
-
interface AuthSession {
|
|
79
|
-
user: { id: string; email: string; name?: string; [key: string]: unknown };
|
|
80
|
-
session: { id: string; expiresAt: Date; [key: string]: unknown };
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
104
|
### Validator Re-exports (from `krutai`)
|
|
85
105
|
|
|
86
106
|
```typescript
|
|
@@ -88,95 +108,87 @@ interface AuthSession {
|
|
|
88
108
|
export { validateApiKeyFormat, validateApiKeyWithService, createApiKeyChecker, ApiKeyValidationError } from 'krutai';
|
|
89
109
|
```
|
|
90
110
|
|
|
91
|
-
|
|
111
|
+
## Usage Examples
|
|
92
112
|
|
|
113
|
+
### Example 1: Standard Server Setup (recommended)
|
|
93
114
|
```typescript
|
|
94
|
-
|
|
95
|
-
|
|
115
|
+
import { krutAuth } from "@krutai/auth";
|
|
116
|
+
import Database from "better-sqlite3";
|
|
96
117
|
|
|
97
|
-
|
|
118
|
+
export const auth = krutAuth({
|
|
119
|
+
database: new Database("./sqlite.db"),
|
|
120
|
+
emailAndPassword: { enabled: true },
|
|
121
|
+
baseURL: process.env.BETTER_AUTH_BASE_URL ?? "http://localhost:3000",
|
|
122
|
+
});
|
|
123
|
+
```
|
|
98
124
|
|
|
99
|
-
### Example
|
|
125
|
+
### Example 2: Next.js Route Handler
|
|
100
126
|
```typescript
|
|
101
|
-
|
|
127
|
+
// app/api/auth/[...all]/route.ts
|
|
128
|
+
import { auth } from "@/lib/auth";
|
|
129
|
+
import { toNextJsHandler } from "@krutai/auth/next-js";
|
|
102
130
|
|
|
103
|
-
export const
|
|
104
|
-
database: { /* config */ },
|
|
105
|
-
});
|
|
131
|
+
export const { GET, POST } = toNextJsHandler(auth);
|
|
106
132
|
```
|
|
107
133
|
|
|
108
|
-
### Example
|
|
134
|
+
### Example 3: React Client
|
|
109
135
|
```typescript
|
|
110
|
-
import {
|
|
136
|
+
import { createAuthClient } from "@krutai/auth/react";
|
|
111
137
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
betterAuthOptions: {
|
|
115
|
-
database: { provider: 'postgres', url: process.env.DATABASE_URL },
|
|
116
|
-
emailAndPassword: { enabled: true },
|
|
117
|
-
},
|
|
138
|
+
export const authClient = createAuthClient({
|
|
139
|
+
baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
|
|
118
140
|
});
|
|
119
|
-
|
|
120
|
-
await auth.initialize();
|
|
121
|
-
const betterAuth = auth.getBetterAuth();
|
|
141
|
+
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
122
142
|
```
|
|
123
143
|
|
|
124
|
-
### Example
|
|
144
|
+
### Example 4: KrutAuth with API Key Validation
|
|
125
145
|
```typescript
|
|
146
|
+
import { KrutAuth } from "@krutai/auth";
|
|
147
|
+
|
|
126
148
|
const auth = new KrutAuth({
|
|
127
149
|
apiKey: process.env.KRUTAI_API_KEY!,
|
|
128
|
-
|
|
129
|
-
betterAuthOptions: { /* config */ },
|
|
150
|
+
betterAuthOptions: { emailAndPassword: { enabled: true } },
|
|
130
151
|
});
|
|
131
|
-
|
|
132
|
-
const betterAuth = auth.getBetterAuth();
|
|
152
|
+
await auth.initialize();
|
|
133
153
|
```
|
|
134
154
|
|
|
135
|
-
### Example
|
|
155
|
+
### Example 5: Error Handling
|
|
136
156
|
```typescript
|
|
137
|
-
import { KrutAuth, ApiKeyValidationError } from
|
|
157
|
+
import { KrutAuth, ApiKeyValidationError } from "@krutai/auth";
|
|
138
158
|
|
|
139
159
|
try {
|
|
140
|
-
const auth = new KrutAuth({ apiKey:
|
|
160
|
+
const auth = new KrutAuth({ apiKey: "bad" });
|
|
141
161
|
await auth.initialize();
|
|
142
162
|
} catch (e) {
|
|
143
163
|
if (e instanceof ApiKeyValidationError) {
|
|
144
|
-
console.error(
|
|
164
|
+
console.error("Invalid API key:", e.message);
|
|
145
165
|
}
|
|
146
166
|
}
|
|
147
167
|
```
|
|
148
168
|
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
// app/api/auth/[...all]/route.ts
|
|
152
|
-
import { auth } from '@/lib/auth';
|
|
153
|
-
import { toNextJsHandler } from '@krutai/auth/next-js';
|
|
169
|
+
## Auto-Migration (postinstall)
|
|
154
170
|
|
|
155
|
-
|
|
156
|
-
```
|
|
171
|
+
`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.
|
|
157
172
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
export const authClient = createAuthClient({
|
|
163
|
-
baseURL: process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000',
|
|
164
|
-
});
|
|
165
|
-
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
166
|
-
```
|
|
173
|
+
The script:
|
|
174
|
+
1. Creates `sqlite.db` in the project root (if not present)
|
|
175
|
+
2. Runs `better-auth migrate` to create all required tables
|
|
176
|
+
3. Skips silently if the DB already exists and tables are up to date
|
|
167
177
|
|
|
168
178
|
## tsup Configuration Notes
|
|
169
179
|
|
|
170
|
-
- `better-auth` →
|
|
171
|
-
- `krutai` → external (peer dep, NOT bundled)
|
|
172
|
-
- `
|
|
180
|
+
- `better-auth` → **external** (real dependency, NOT bundled)
|
|
181
|
+
- `krutai` → **external** (peer dep, NOT bundled)
|
|
182
|
+
- `better-sqlite3` → **external** (real dependency)
|
|
183
|
+
- `react`, `react-dom`, `next` → external
|
|
173
184
|
|
|
174
185
|
## Important Notes
|
|
175
186
|
|
|
176
|
-
1. **
|
|
177
|
-
2.
|
|
178
|
-
3.
|
|
179
|
-
4. **
|
|
187
|
+
1. **Always set `baseURL`**: Pass `baseURL: process.env.BETTER_AUTH_BASE_URL` in `krutAuth()` options
|
|
188
|
+
2. **Use `krutAuth` not `betterAuth`**: The public API is `krutAuth`. `betterAuth` is an internal implementation detail
|
|
189
|
+
3. **Validator lives in `krutai`**: Never add a local `validator.ts` — import from `krutai`
|
|
190
|
+
4. **No `noExternal` for `better-auth` or `krutai`**: They must stay external in tsup
|
|
191
|
+
5. **`getBetterAuth()` returns `Auth`**: Uses the `Auth` type from `better-auth`, not `ReturnType<typeof betterAuth>`
|
|
180
192
|
|
|
181
193
|
## Related Packages
|
|
182
194
|
|
package/README.md
CHANGED
|
@@ -6,7 +6,8 @@ Authentication package for KrutAI powered by [Better Auth](https://www.better-au
|
|
|
6
6
|
|
|
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
|
-
- 📦 **Auto-installs
|
|
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`
|
|
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
|
|
|
@@ -25,15 +26,20 @@ npm install @krutai/auth
|
|
|
25
26
|
|
|
26
27
|
```typescript
|
|
27
28
|
// lib/auth.ts
|
|
28
|
-
import {
|
|
29
|
+
import { krutAuth } from "@krutai/auth";
|
|
30
|
+
import Database from "better-sqlite3";
|
|
29
31
|
|
|
30
|
-
export const auth =
|
|
31
|
-
database:
|
|
32
|
-
|
|
32
|
+
export const auth = krutAuth({
|
|
33
|
+
database: new Database("./sqlite.db"),
|
|
34
|
+
emailAndPassword: {
|
|
35
|
+
enabled: true,
|
|
33
36
|
},
|
|
37
|
+
baseURL: process.env.BETTER_AUTH_BASE_URL ?? "http://localhost:3000",
|
|
34
38
|
});
|
|
35
39
|
```
|
|
36
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
|
+
|
|
37
43
|
### API Route handler
|
|
38
44
|
|
|
39
45
|
```typescript
|
|
@@ -57,7 +63,7 @@ export const authClient = createAuthClient({
|
|
|
57
63
|
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
58
64
|
```
|
|
59
65
|
|
|
60
|
-
### With API Key Validation
|
|
66
|
+
### With API Key Validation (KrutAuth class)
|
|
61
67
|
|
|
62
68
|
```typescript
|
|
63
69
|
import { KrutAuth } from "@krutai/auth";
|
|
@@ -72,26 +78,42 @@ const auth = new KrutAuth({
|
|
|
72
78
|
await auth.initialize();
|
|
73
79
|
```
|
|
74
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
|
+
|
|
75
88
|
## Exports
|
|
76
89
|
|
|
77
90
|
| Import path | What it provides |
|
|
78
91
|
|---|---|
|
|
79
|
-
| `@krutai/auth` | `
|
|
92
|
+
| `@krutai/auth` | `krutAuth`, `KrutAuth`, validators |
|
|
80
93
|
| `@krutai/auth/react` | `createAuthClient`, `useSession`, etc. |
|
|
81
94
|
| `@krutai/auth/next-js` | `toNextJsHandler` |
|
|
82
95
|
|
|
83
96
|
## API Reference
|
|
84
97
|
|
|
85
|
-
### `
|
|
98
|
+
### `krutAuth(options)`
|
|
86
99
|
|
|
87
|
-
|
|
100
|
+
Drop-in replacement for `betterAuth`. Accepts the same options.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { krutAuth } from "@krutai/auth";
|
|
104
|
+
|
|
105
|
+
export const auth = krutAuth({ /* Better Auth options */ });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `KrutAuth` class
|
|
109
|
+
|
|
110
|
+
For API-key-protected authentication.
|
|
88
111
|
|
|
89
112
|
| Option | Type | Required | Description |
|
|
90
113
|
|---|---|---|---|
|
|
91
114
|
| `apiKey` | `string` | ✅ | Your KrutAI API key |
|
|
92
115
|
| `betterAuthOptions` | `object` | — | Better Auth configuration |
|
|
93
116
|
| `validateOnInit` | `boolean` | — | Validate API key on init (default: `true`) |
|
|
94
|
-
| `validationEndpoint` | `string` | — | Custom validation endpoint |
|
|
95
117
|
|
|
96
118
|
#### Methods
|
|
97
119
|
|
|
@@ -115,17 +137,14 @@ try {
|
|
|
115
137
|
}
|
|
116
138
|
```
|
|
117
139
|
|
|
118
|
-
> `ApiKeyValidationError` is re-exported from `krutai` — the single source of truth for API validation across the KrutAI ecosystem.
|
|
119
|
-
|
|
120
140
|
## Architecture
|
|
121
141
|
|
|
122
|
-
`@krutai/auth` depends on `krutai` (auto-installed as a peer dependency) for API key validation. `better-auth` is bundled into the output and `better-sqlite3` is auto-installed as a dependency.
|
|
123
|
-
|
|
124
142
|
```
|
|
125
|
-
@krutai/auth@0.1.
|
|
126
|
-
├──
|
|
127
|
-
├── dependency:
|
|
128
|
-
|
|
143
|
+
@krutai/auth@0.1.8
|
|
144
|
+
├── dependency: krutai ← API key validation
|
|
145
|
+
├── dependency: better-auth ← auth engine
|
|
146
|
+
├── dependency: better-sqlite3 ← default database adapter
|
|
147
|
+
└── dependency: @types/better-sqlite3
|
|
129
148
|
```
|
|
130
149
|
|
|
131
150
|
For Better Auth documentation, visit: https://www.better-auth.com/docs
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
1
2
|
import { BetterAuthOptions, Auth } from 'better-auth';
|
|
2
3
|
export { BetterAuthOptions } from 'better-auth';
|
|
3
4
|
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
@@ -122,35 +123,23 @@ declare class KrutAuth {
|
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
/**
|
|
125
|
-
*
|
|
126
|
+
* krutAuth — drop-in replacement for betterAuth.
|
|
126
127
|
*
|
|
127
|
-
*
|
|
128
|
+
* Use this instead of importing betterAuth directly.
|
|
128
129
|
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* import { KrutAuth } from "@krutai/auth";
|
|
132
|
-
*
|
|
133
|
-
* export const auth = new KrutAuth({
|
|
134
|
-
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
135
|
-
* betterAuthOptions: { ... },
|
|
136
|
-
* });
|
|
137
|
-
* await auth.initialize();
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* @example Client-side (React / Next.js client component)
|
|
130
|
+
* @example
|
|
141
131
|
* ```typescript
|
|
142
|
-
* import {
|
|
132
|
+
* import { krutAuth } from "@krutai/auth";
|
|
133
|
+
* import Database from "better-sqlite3";
|
|
143
134
|
*
|
|
144
|
-
* export const
|
|
145
|
-
*
|
|
135
|
+
* export const auth = krutAuth({
|
|
136
|
+
* database: new Database("./sqlite.db"),
|
|
137
|
+
* emailAndPassword: { enabled: true },
|
|
146
138
|
* });
|
|
147
|
-
*
|
|
148
|
-
* export const { signIn, signUp, signOut, useSession } = authClient;
|
|
149
139
|
* ```
|
|
150
|
-
*
|
|
151
|
-
* @packageDocumentation
|
|
152
140
|
*/
|
|
141
|
+
declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
|
|
153
142
|
|
|
154
|
-
declare const VERSION = "0.1.
|
|
143
|
+
declare const VERSION = "0.1.8";
|
|
155
144
|
|
|
156
|
-
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
|
|
145
|
+
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
1
2
|
import { BetterAuthOptions, Auth } from 'better-auth';
|
|
2
3
|
export { BetterAuthOptions } from 'better-auth';
|
|
3
4
|
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
@@ -122,35 +123,23 @@ declare class KrutAuth {
|
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
/**
|
|
125
|
-
*
|
|
126
|
+
* krutAuth — drop-in replacement for betterAuth.
|
|
126
127
|
*
|
|
127
|
-
*
|
|
128
|
+
* Use this instead of importing betterAuth directly.
|
|
128
129
|
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* import { KrutAuth } from "@krutai/auth";
|
|
132
|
-
*
|
|
133
|
-
* export const auth = new KrutAuth({
|
|
134
|
-
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
135
|
-
* betterAuthOptions: { ... },
|
|
136
|
-
* });
|
|
137
|
-
* await auth.initialize();
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* @example Client-side (React / Next.js client component)
|
|
130
|
+
* @example
|
|
141
131
|
* ```typescript
|
|
142
|
-
* import {
|
|
132
|
+
* import { krutAuth } from "@krutai/auth";
|
|
133
|
+
* import Database from "better-sqlite3";
|
|
143
134
|
*
|
|
144
|
-
* export const
|
|
145
|
-
*
|
|
135
|
+
* export const auth = krutAuth({
|
|
136
|
+
* database: new Database("./sqlite.db"),
|
|
137
|
+
* emailAndPassword: { enabled: true },
|
|
146
138
|
* });
|
|
147
|
-
*
|
|
148
|
-
* export const { signIn, signUp, signOut, useSession } = authClient;
|
|
149
139
|
* ```
|
|
150
|
-
*
|
|
151
|
-
* @packageDocumentation
|
|
152
140
|
*/
|
|
141
|
+
declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
|
|
153
142
|
|
|
154
|
-
declare const VERSION = "0.1.
|
|
143
|
+
declare const VERSION = "0.1.8";
|
|
155
144
|
|
|
156
|
-
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
|
|
145
|
+
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var betterAuth = require('better-auth');
|
|
4
4
|
var krutai = require('krutai');
|
|
5
5
|
|
|
6
|
-
// src/
|
|
6
|
+
// src/index.ts
|
|
7
7
|
var KrutAuth = class {
|
|
8
8
|
/**
|
|
9
9
|
* Creates a new KrutAuth instance
|
|
@@ -93,7 +93,10 @@ var KrutAuth = class {
|
|
|
93
93
|
return this.getBetterAuth();
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
|
-
|
|
96
|
+
function krutAuth(options) {
|
|
97
|
+
return betterAuth.betterAuth(options);
|
|
98
|
+
}
|
|
99
|
+
var VERSION = "0.1.8";
|
|
97
100
|
|
|
98
101
|
Object.defineProperty(exports, "ApiKeyValidationError", {
|
|
99
102
|
enumerable: true,
|
|
@@ -113,5 +116,6 @@ Object.defineProperty(exports, "validateApiKeyWithService", {
|
|
|
113
116
|
});
|
|
114
117
|
exports.KrutAuth = KrutAuth;
|
|
115
118
|
exports.VERSION = VERSION;
|
|
119
|
+
exports.krutAuth = krutAuth;
|
|
116
120
|
//# sourceMappingURL=index.js.map
|
|
117
121
|
//# sourceMappingURL=index.js.map
|
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;A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C,IAAA,IAAA,CAAK,qBAAqBC,qBAAA,CAAW;AAAA,MACjC,GAAG,KAAK,MAAA,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;AAiBO,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 validator utilities from krutai peer dependency\nexport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n createApiKeyChecker,\n ApiKeyValidationError,\n} from 'krutai';\n\n// Package metadata\nexport const VERSION = '0.1.8';\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { betterAuth } from 'better-auth';
|
|
|
2
2
|
import { validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
3
3
|
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
4
4
|
|
|
5
|
-
// src/
|
|
5
|
+
// src/index.ts
|
|
6
6
|
var KrutAuth = class {
|
|
7
7
|
/**
|
|
8
8
|
* Creates a new KrutAuth instance
|
|
@@ -92,8 +92,11 @@ var KrutAuth = class {
|
|
|
92
92
|
return this.getBetterAuth();
|
|
93
93
|
}
|
|
94
94
|
};
|
|
95
|
-
|
|
95
|
+
function krutAuth(options) {
|
|
96
|
+
return betterAuth(options);
|
|
97
|
+
}
|
|
98
|
+
var VERSION = "0.1.8";
|
|
96
99
|
|
|
97
|
-
export { KrutAuth, VERSION };
|
|
100
|
+
export { KrutAuth, VERSION, krutAuth };
|
|
98
101
|
//# sourceMappingURL=index.mjs.map
|
|
99
102
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/index.ts"],"names":[],"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,E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A;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,WAAW,OAAO,CAAA;AAC7B;AAiBO,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 validator utilities from krutai peer dependency\nexport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n createApiKeyChecker,\n ApiKeyValidationError,\n} from 'krutai';\n\n// Package metadata\nexport const VERSION = '0.1.8';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krutai/auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
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
|
+
}
|