@krutai/auth 0.1.2 → 0.1.4
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 +96 -148
- package/README.md +66 -136
- package/dist/index.d.mts +3 -42
- package/dist/index.d.ts +3 -42
- package/dist/index.js +20 -66
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -61
- package/dist/index.mjs.map +1 -1
- package/dist/next-js.d.mts +1 -0
- package/dist/next-js.d.ts +1 -0
- package/dist/next-js.js +7717 -0
- package/dist/next-js.js.map +1 -0
- package/dist/next-js.mjs +7714 -0
- package/dist/next-js.mjs.map +1 -0
- package/package.json +15 -3
package/AI_REFERENCE.md
CHANGED
|
@@ -1,33 +1,53 @@
|
|
|
1
|
-
# @krutai/auth
|
|
1
|
+
# @krutai/auth — AI Assistant Reference Guide
|
|
2
2
|
|
|
3
3
|
## Package Overview
|
|
4
4
|
|
|
5
|
-
`@krutai/auth`
|
|
5
|
+
- **Name**: `@krutai/auth`
|
|
6
|
+
- **Version**: `0.1.4`
|
|
7
|
+
- **Purpose**: Authentication package for KrutAI — wraps Better Auth with mandatory API key validation
|
|
8
|
+
- **Entry**: `src/index.ts` → `dist/index.{js,mjs,d.ts}`
|
|
9
|
+
- **Build**: `tsup` (CJS + ESM, `better-auth` bundled, `krutai` external peer dep)
|
|
6
10
|
|
|
7
|
-
##
|
|
11
|
+
## Dependency Architecture
|
|
8
12
|
|
|
9
|
-
```bash
|
|
10
|
-
npm install @krutai/auth
|
|
11
13
|
```
|
|
14
|
+
@krutai/auth@0.1.4
|
|
15
|
+
├── peerDependency: krutai >=0.1.2 ← auto-installed, provides API validation
|
|
16
|
+
├── dependency: better-sqlite3 ← auto-installed
|
|
17
|
+
└── bundled: better-auth ← included in dist (noExternal)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
> **Important for AI**: The validator (`validateApiKeyFormat`, `ApiKeyValidationError`, etc.) is NOT defined in this package. It is imported from `krutai` and re-exported. Do NOT add a local `validator.ts` here.
|
|
12
21
|
|
|
13
|
-
##
|
|
22
|
+
## File Structure
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
packages/auth/
|
|
26
|
+
├── src/
|
|
27
|
+
│ ├── index.ts # Barrel export — re-exports from krutai for validator
|
|
28
|
+
│ ├── client.ts # KrutAuth class
|
|
29
|
+
│ ├── types.ts # KrutAuthConfig, AuthSession, BetterAuthOptions
|
|
30
|
+
│ ├── react.ts # createAuthClient (better-auth/react)
|
|
31
|
+
│ └── next-js.ts # toNextJsHandler (better-auth/next-js)
|
|
32
|
+
├── package.json
|
|
33
|
+
├── tsconfig.json
|
|
34
|
+
└── tsup.config.ts
|
|
35
|
+
```
|
|
14
36
|
|
|
15
|
-
|
|
16
|
-
- **MANDATORY**: All users must provide a valid API key
|
|
17
|
-
- API key is validated on initialization (can be disabled with `validateOnInit: false`)
|
|
18
|
-
- Throws `ApiKeyValidationError` if API key is missing or invalid
|
|
37
|
+
## Sub-path Exports
|
|
19
38
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
39
|
+
| Import | File | Purpose |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `@krutai/auth` | `dist/index.js` | Server-side: `betterAuth`, `KrutAuth`, validator re-exports |
|
|
42
|
+
| `@krutai/auth/react` | `dist/react.js` | Client-side: `createAuthClient`, hooks |
|
|
43
|
+
| `@krutai/auth/next-js` | `dist/next-js.js` | Next.js handler: `toNextJsHandler` |
|
|
24
44
|
|
|
25
45
|
## Main Exports
|
|
26
46
|
|
|
27
47
|
### Classes
|
|
28
48
|
|
|
29
49
|
#### `KrutAuth`
|
|
30
|
-
Main authentication client
|
|
50
|
+
Main authentication client.
|
|
31
51
|
|
|
32
52
|
**Constructor:**
|
|
33
53
|
```typescript
|
|
@@ -35,13 +55,11 @@ new KrutAuth(config: KrutAuthConfig)
|
|
|
35
55
|
```
|
|
36
56
|
|
|
37
57
|
**Methods:**
|
|
38
|
-
- `initialize(): Promise<void>`
|
|
39
|
-
- `getBetterAuth()
|
|
40
|
-
- `isInitialized(): boolean`
|
|
41
|
-
- `getApiKey(): string`
|
|
42
|
-
- `signIn()`
|
|
43
|
-
- `signOut()` - Convenience method (returns Better Auth instance)
|
|
44
|
-
- `getSession()` - Convenience method (returns Better Auth instance)
|
|
58
|
+
- `initialize(): Promise<void>` — validates API key + initializes Better Auth
|
|
59
|
+
- `getBetterAuth()` — returns the Better Auth instance
|
|
60
|
+
- `isInitialized(): boolean`
|
|
61
|
+
- `getApiKey(): string`
|
|
62
|
+
- `signIn()`, `signOut()`, `getSession()` — convenience wrappers
|
|
45
63
|
|
|
46
64
|
### Types
|
|
47
65
|
|
|
@@ -58,185 +76,115 @@ interface KrutAuthConfig {
|
|
|
58
76
|
#### `AuthSession`
|
|
59
77
|
```typescript
|
|
60
78
|
interface AuthSession {
|
|
61
|
-
user: {
|
|
62
|
-
|
|
63
|
-
email: string;
|
|
64
|
-
name?: string;
|
|
65
|
-
[key: string]: unknown;
|
|
66
|
-
};
|
|
67
|
-
session: {
|
|
68
|
-
id: string;
|
|
69
|
-
expiresAt: Date;
|
|
70
|
-
[key: string]: unknown;
|
|
71
|
-
};
|
|
79
|
+
user: { id: string; email: string; name?: string; [key: string]: unknown };
|
|
80
|
+
session: { id: string; expiresAt: Date; [key: string]: unknown };
|
|
72
81
|
}
|
|
73
82
|
```
|
|
74
83
|
|
|
75
|
-
###
|
|
84
|
+
### Validator Re-exports (from `krutai`)
|
|
76
85
|
|
|
77
|
-
|
|
78
|
-
|
|
86
|
+
```typescript
|
|
87
|
+
// These are re-exported from krutai — NOT defined here
|
|
88
|
+
export { validateApiKeyFormat, validateApiKeyWithService, createApiKeyChecker, ApiKeyValidationError } from 'krutai';
|
|
89
|
+
```
|
|
79
90
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
91
|
+
### Other Re-exports
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
export { betterAuth } from 'better-auth';
|
|
95
|
+
```
|
|
84
96
|
|
|
85
97
|
## Usage Examples
|
|
86
98
|
|
|
87
|
-
### Example 1: Basic
|
|
99
|
+
### Example 1: Basic Server Setup
|
|
100
|
+
```typescript
|
|
101
|
+
import { betterAuth } from '@krutai/auth';
|
|
88
102
|
|
|
103
|
+
export const auth = betterAuth({
|
|
104
|
+
database: { /* config */ },
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Example 2: KrutAuth with API Key
|
|
89
109
|
```typescript
|
|
90
110
|
import { KrutAuth } from '@krutai/auth';
|
|
91
111
|
|
|
92
112
|
const auth = new KrutAuth({
|
|
93
|
-
apiKey:
|
|
113
|
+
apiKey: process.env.KRUTAI_API_KEY!,
|
|
94
114
|
betterAuthOptions: {
|
|
95
|
-
database: {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
115
|
+
database: { provider: 'postgres', url: process.env.DATABASE_URL },
|
|
116
|
+
emailAndPassword: { enabled: true },
|
|
117
|
+
},
|
|
99
118
|
});
|
|
100
119
|
|
|
101
|
-
// Initialize and validate API key
|
|
102
120
|
await auth.initialize();
|
|
103
|
-
|
|
104
|
-
// Get Better Auth instance
|
|
105
121
|
const betterAuth = auth.getBetterAuth();
|
|
106
122
|
```
|
|
107
123
|
|
|
108
|
-
### Example
|
|
109
|
-
|
|
124
|
+
### Example 3: Skip Async Validation
|
|
110
125
|
```typescript
|
|
111
|
-
import { KrutAuth } from '@krutai/auth';
|
|
112
|
-
|
|
113
126
|
const auth = new KrutAuth({
|
|
114
|
-
apiKey:
|
|
127
|
+
apiKey: process.env.KRUTAI_API_KEY!,
|
|
115
128
|
validateOnInit: false,
|
|
116
|
-
betterAuthOptions: {
|
|
117
|
-
// config
|
|
118
|
-
}
|
|
129
|
+
betterAuthOptions: { /* config */ },
|
|
119
130
|
});
|
|
120
|
-
|
|
121
131
|
// No need to call initialize()
|
|
122
132
|
const betterAuth = auth.getBetterAuth();
|
|
123
133
|
```
|
|
124
134
|
|
|
125
|
-
### Example
|
|
126
|
-
|
|
135
|
+
### Example 4: Error Handling
|
|
127
136
|
```typescript
|
|
128
137
|
import { KrutAuth, ApiKeyValidationError } from '@krutai/auth';
|
|
129
138
|
|
|
130
139
|
try {
|
|
131
|
-
const auth = new KrutAuth({
|
|
132
|
-
apiKey: 'invalid-key'
|
|
133
|
-
});
|
|
140
|
+
const auth = new KrutAuth({ apiKey: 'bad' });
|
|
134
141
|
await auth.initialize();
|
|
135
|
-
} catch (
|
|
136
|
-
if (
|
|
137
|
-
console.error('API key
|
|
142
|
+
} catch (e) {
|
|
143
|
+
if (e instanceof ApiKeyValidationError) {
|
|
144
|
+
console.error('Invalid API key:', e.message);
|
|
138
145
|
}
|
|
139
146
|
}
|
|
140
147
|
```
|
|
141
148
|
|
|
142
|
-
### Example
|
|
143
|
-
|
|
149
|
+
### Example 5: Next.js Route Handler
|
|
144
150
|
```typescript
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
apiKey: process.env.KRUTAI_API_KEY!,
|
|
149
|
-
betterAuthOptions: {
|
|
150
|
-
database: {
|
|
151
|
-
provider: 'postgres',
|
|
152
|
-
url: process.env.DATABASE_URL
|
|
153
|
-
},
|
|
154
|
-
emailAndPassword: {
|
|
155
|
-
enabled: true
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
await auth.initialize();
|
|
161
|
-
|
|
162
|
-
// Access full Better Auth API
|
|
163
|
-
const betterAuth = auth.getBetterAuth();
|
|
151
|
+
// app/api/auth/[...all]/route.ts
|
|
152
|
+
import { auth } from '@/lib/auth';
|
|
153
|
+
import { toNextJsHandler } from '@krutai/auth/next-js';
|
|
164
154
|
|
|
165
|
-
|
|
166
|
-
// See: https://www.better-auth.com/docs
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
## Common Patterns
|
|
170
|
-
|
|
171
|
-
### Pattern 1: Environment Variable API Key
|
|
172
|
-
|
|
173
|
-
```typescript
|
|
174
|
-
const auth = new KrutAuth({
|
|
175
|
-
apiKey: process.env.KRUTAI_API_KEY || '',
|
|
176
|
-
betterAuthOptions: {
|
|
177
|
-
// config
|
|
178
|
-
}
|
|
179
|
-
});
|
|
155
|
+
export const { GET, POST } = toNextJsHandler(auth);
|
|
180
156
|
```
|
|
181
157
|
|
|
182
|
-
###
|
|
183
|
-
|
|
158
|
+
### Example 6: React Client
|
|
184
159
|
```typescript
|
|
185
|
-
|
|
186
|
-
let authInstance: KrutAuth | null = null;
|
|
187
|
-
|
|
188
|
-
export async function getAuth(): Promise<KrutAuth> {
|
|
189
|
-
if (!authInstance) {
|
|
190
|
-
authInstance = new KrutAuth({
|
|
191
|
-
apiKey: process.env.KRUTAI_API_KEY!,
|
|
192
|
-
betterAuthOptions: {
|
|
193
|
-
// config
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
await authInstance.initialize();
|
|
197
|
-
}
|
|
198
|
-
return authInstance;
|
|
199
|
-
}
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### Pattern 3: Conditional Initialization
|
|
160
|
+
import { createAuthClient } from '@krutai/auth/react';
|
|
203
161
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
apiKey: process.env.KRUTAI_API_KEY!,
|
|
207
|
-
validateOnInit: process.env.NODE_ENV === 'production'
|
|
162
|
+
export const authClient = createAuthClient({
|
|
163
|
+
baseURL: process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000',
|
|
208
164
|
});
|
|
209
|
-
|
|
210
|
-
if (process.env.NODE_ENV === 'production') {
|
|
211
|
-
await auth.initialize();
|
|
212
|
-
}
|
|
165
|
+
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
213
166
|
```
|
|
214
167
|
|
|
215
|
-
##
|
|
168
|
+
## tsup Configuration Notes
|
|
216
169
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
import type {
|
|
221
|
-
KrutAuthConfig,
|
|
222
|
-
AuthSession,
|
|
223
|
-
BetterAuthOptions
|
|
224
|
-
} from '@krutai/auth';
|
|
225
|
-
```
|
|
170
|
+
- `better-auth` → `noExternal` (bundled into dist)
|
|
171
|
+
- `krutai` → external (peer dep, NOT bundled)
|
|
172
|
+
- `react`, `react-dom`, `next`, `better-sqlite3` → external
|
|
226
173
|
|
|
227
174
|
## Important Notes
|
|
228
175
|
|
|
229
|
-
1. **
|
|
230
|
-
2.
|
|
231
|
-
3.
|
|
232
|
-
4. **
|
|
176
|
+
1. **Validator lives in `krutai`**: Never add a local `validator.ts` — import from `krutai`
|
|
177
|
+
2. **`krutai` must be external in tsup**: Do NOT add it to `noExternal`
|
|
178
|
+
3. **`krutai` in devDependencies**: Needed for local TypeScript compilation during development
|
|
179
|
+
4. **API key validation**: Format check is synchronous (constructor), service check is async (`initialize()`)
|
|
233
180
|
|
|
234
181
|
## Related Packages
|
|
235
182
|
|
|
236
|
-
- `krutai`
|
|
237
|
-
-
|
|
183
|
+
- `krutai` — Core utilities and API validation (peer dep)
|
|
184
|
+
- `@krutai/rbac` — Role-Based Access Control
|
|
238
185
|
|
|
239
186
|
## Links
|
|
240
187
|
|
|
241
|
-
- Better Auth
|
|
242
|
-
- GitHub
|
|
188
|
+
- Better Auth Docs: https://www.better-auth.com/docs
|
|
189
|
+
- GitHub: https://github.com/AccountantAIOrg/krut_packages
|
|
190
|
+
- npm: https://www.npmjs.com/package/@krutai/auth
|
package/README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
# @krutai/auth
|
|
2
2
|
|
|
3
|
-
Authentication package for KrutAI powered by [
|
|
3
|
+
Authentication package for KrutAI powered by [Better Auth](https://www.better-auth.com/).
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- 🔐 **API Key Protection**
|
|
8
|
-
- 🚀 **Better Auth Integration**
|
|
9
|
-
- 📦 **
|
|
10
|
-
- 🎯 **
|
|
11
|
-
- ⚡ **Dual Format**
|
|
7
|
+
- 🔐 **API Key Protection** — Requires a valid KrutAI API key (validated via `krutai`)
|
|
8
|
+
- 🚀 **Better Auth Integration** — Built on top of Better Auth
|
|
9
|
+
- 📦 **Auto-installs `krutai`** — The core `krutai` package is installed automatically as a peer dependency
|
|
10
|
+
- 🎯 **Next.js Ready** — First-class support via `@krutai/auth/next-js`
|
|
11
|
+
- ⚡ **Dual Format** — Supports both ESM and CommonJS
|
|
12
|
+
- 🔷 **TypeScript First** — Full type safety and IntelliSense
|
|
12
13
|
|
|
13
14
|
## Installation
|
|
14
15
|
|
|
@@ -16,190 +17,119 @@ Authentication package for KrutAI powered by [Krut AI](https://www.krut.ai/).
|
|
|
16
17
|
npm install @krutai/auth
|
|
17
18
|
```
|
|
18
19
|
|
|
19
|
-
> **Note:**
|
|
20
|
+
> **Note:** `krutai` is automatically installed as a peer dependency. `better-sqlite3` is included as a dependency and `better-auth` is bundled — no additional packages required.
|
|
20
21
|
|
|
21
22
|
## Quick Start
|
|
22
23
|
|
|
23
|
-
###
|
|
24
|
+
### Server-side setup (Next.js)
|
|
24
25
|
|
|
25
26
|
```typescript
|
|
26
|
-
|
|
27
|
+
// lib/auth.ts
|
|
28
|
+
import { betterAuth } from "@krutai/auth";
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
betterAuthOptions: {
|
|
32
|
-
// Better Auth configuration
|
|
33
|
-
database: {
|
|
34
|
-
// Your database configuration
|
|
35
|
-
},
|
|
30
|
+
export const auth = betterAuth({
|
|
31
|
+
database: {
|
|
32
|
+
// your database config
|
|
36
33
|
},
|
|
37
34
|
});
|
|
38
|
-
|
|
39
|
-
// Initialize the client (validates API key)
|
|
40
|
-
await auth.initialize();
|
|
41
|
-
|
|
42
|
-
// Use authentication features
|
|
43
|
-
const session = await auth.getSession();
|
|
44
35
|
```
|
|
45
36
|
|
|
46
|
-
###
|
|
47
|
-
|
|
48
|
-
If you want to skip async validation on initialization:
|
|
37
|
+
### API Route handler
|
|
49
38
|
|
|
50
39
|
```typescript
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
betterAuthOptions: {
|
|
55
|
-
// Better Auth configuration
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// No need to call initialize()
|
|
60
|
-
const betterAuth = auth.getBetterAuth();
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## API Reference
|
|
64
|
-
|
|
65
|
-
### `KrutAuth`
|
|
40
|
+
// app/api/auth/[...all]/route.ts
|
|
41
|
+
import { auth } from "@/lib/auth";
|
|
42
|
+
import { toNextJsHandler } from "@krutai/auth/next-js";
|
|
66
43
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
#### Constructor
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
new KrutAuth(config: KrutAuthConfig)
|
|
44
|
+
export const { GET, POST } = toNextJsHandler(auth);
|
|
73
45
|
```
|
|
74
46
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
- `config.apiKey` (required): Your KrutAI API key
|
|
78
|
-
- `config.betterAuthOptions` (optional): Better Auth configuration options
|
|
79
|
-
- `config.validateOnInit` (optional): Whether to validate API key on initialization (default: `true`)
|
|
80
|
-
- `config.validationEndpoint` (optional): Custom API validation endpoint
|
|
81
|
-
|
|
82
|
-
**Throws:**
|
|
83
|
-
|
|
84
|
-
- `ApiKeyValidationError` if the API key format is invalid
|
|
85
|
-
|
|
86
|
-
#### Methods
|
|
87
|
-
|
|
88
|
-
##### `initialize()`
|
|
89
|
-
|
|
90
|
-
Validates the API key and initializes the Better Auth instance.
|
|
47
|
+
### Client-side (React / Next.js)
|
|
91
48
|
|
|
92
49
|
```typescript
|
|
93
|
-
|
|
94
|
-
|
|
50
|
+
// lib/auth-client.ts
|
|
51
|
+
import { createAuthClient } from "@krutai/auth/react";
|
|
95
52
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
##### `getBetterAuth()`
|
|
101
|
-
|
|
102
|
-
Gets the underlying Better Auth instance for advanced usage.
|
|
53
|
+
export const authClient = createAuthClient({
|
|
54
|
+
baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
|
|
55
|
+
});
|
|
103
56
|
|
|
104
|
-
|
|
105
|
-
const betterAuth = auth.getBetterAuth();
|
|
57
|
+
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
106
58
|
```
|
|
107
59
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
**Throws:** `Error` if not initialized
|
|
111
|
-
|
|
112
|
-
##### `isInitialized()`
|
|
113
|
-
|
|
114
|
-
Checks if the client is initialized.
|
|
60
|
+
### With API Key Validation
|
|
115
61
|
|
|
116
62
|
```typescript
|
|
117
|
-
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
**Returns:** `boolean`
|
|
63
|
+
import { KrutAuth } from "@krutai/auth";
|
|
121
64
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
65
|
+
const auth = new KrutAuth({
|
|
66
|
+
apiKey: "your-krutai-api-key",
|
|
67
|
+
betterAuthOptions: {
|
|
68
|
+
database: { /* your database config */ },
|
|
69
|
+
},
|
|
70
|
+
});
|
|
125
71
|
|
|
126
|
-
|
|
127
|
-
const apiKey = auth.getApiKey();
|
|
72
|
+
await auth.initialize();
|
|
128
73
|
```
|
|
129
74
|
|
|
130
|
-
|
|
75
|
+
## Exports
|
|
131
76
|
|
|
132
|
-
|
|
77
|
+
| Import path | What it provides |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `@krutai/auth` | `betterAuth`, `KrutAuth`, validator re-exports from `krutai` |
|
|
80
|
+
| `@krutai/auth/react` | `createAuthClient`, `useSession`, etc. |
|
|
81
|
+
| `@krutai/auth/next-js` | `toNextJsHandler` |
|
|
133
82
|
|
|
134
|
-
|
|
83
|
+
## API Reference
|
|
135
84
|
|
|
136
|
-
|
|
85
|
+
### `KrutAuth`
|
|
137
86
|
|
|
138
|
-
|
|
139
|
-
@krutai/auth
|
|
140
|
-
└── krutai (parent)
|
|
141
|
-
└── Provides: validateApiKeyFormat, validateApiKeyWithService, etc.
|
|
142
|
-
```
|
|
87
|
+
#### Constructor options
|
|
143
88
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
89
|
+
| Option | Type | Required | Description |
|
|
90
|
+
|---|---|---|---|
|
|
91
|
+
| `apiKey` | `string` | ✅ | Your KrutAI API key |
|
|
92
|
+
| `betterAuthOptions` | `object` | — | Better Auth configuration |
|
|
93
|
+
| `validateOnInit` | `boolean` | — | Validate API key on init (default: `true`) |
|
|
94
|
+
| `validationEndpoint` | `string` | — | Custom validation endpoint |
|
|
149
95
|
|
|
150
|
-
|
|
96
|
+
#### Methods
|
|
151
97
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
```
|
|
98
|
+
- `initialize()` — Validates API key and sets up Better Auth
|
|
99
|
+
- `getBetterAuth()` — Returns the underlying Better Auth instance
|
|
100
|
+
- `isInitialized()` — Returns `boolean`
|
|
101
|
+
- `getApiKey()` — Returns the API key string
|
|
157
102
|
|
|
158
103
|
## Error Handling
|
|
159
104
|
|
|
160
|
-
The package throws `ApiKeyValidationError` (from parent `krutai` package) when:
|
|
161
|
-
|
|
162
|
-
- API key is not provided
|
|
163
|
-
- API key is empty or invalid format
|
|
164
|
-
- API key validation fails (if `validateOnInit` is `true`)
|
|
165
|
-
|
|
166
105
|
```typescript
|
|
167
|
-
import { KrutAuth, ApiKeyValidationError } from
|
|
106
|
+
import { KrutAuth, ApiKeyValidationError } from "@krutai/auth";
|
|
168
107
|
|
|
169
108
|
try {
|
|
170
|
-
const auth = new KrutAuth({
|
|
171
|
-
apiKey: 'invalid-key',
|
|
172
|
-
});
|
|
109
|
+
const auth = new KrutAuth({ apiKey: "invalid" });
|
|
173
110
|
await auth.initialize();
|
|
174
111
|
} catch (error) {
|
|
175
112
|
if (error instanceof ApiKeyValidationError) {
|
|
176
|
-
console.error(
|
|
113
|
+
console.error("API key validation failed:", error.message);
|
|
177
114
|
}
|
|
178
115
|
}
|
|
179
116
|
```
|
|
180
117
|
|
|
181
|
-
|
|
118
|
+
> `ApiKeyValidationError` is re-exported from `krutai` — the single source of truth for API validation across the KrutAI ecosystem.
|
|
182
119
|
|
|
183
|
-
|
|
120
|
+
## Architecture
|
|
184
121
|
|
|
185
|
-
|
|
186
|
-
const auth = new KrutAuth({ apiKey: 'your-key' });
|
|
187
|
-
await auth.initialize();
|
|
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.
|
|
188
123
|
|
|
189
|
-
|
|
190
|
-
|
|
124
|
+
```
|
|
125
|
+
@krutai/auth@0.1.4
|
|
126
|
+
├── peerDependency: krutai >=0.1.2 (auto-installed)
|
|
127
|
+
├── dependency: better-sqlite3
|
|
128
|
+
└── bundled: better-auth
|
|
191
129
|
```
|
|
192
130
|
|
|
193
131
|
For Better Auth documentation, visit: https://www.better-auth.com/docs
|
|
194
132
|
|
|
195
|
-
## TypeScript Support
|
|
196
|
-
|
|
197
|
-
Full TypeScript support with exported types:
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
import type { KrutAuthConfig, AuthSession, BetterAuthOptions } from '@krutai/auth';
|
|
201
|
-
```
|
|
202
|
-
|
|
203
133
|
## License
|
|
204
134
|
|
|
205
135
|
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as better_auth from 'better-auth';
|
|
2
2
|
import { BetterAuthOptions, betterAuth } from 'better-auth';
|
|
3
3
|
export { BetterAuthOptions, betterAuth } from 'better-auth';
|
|
4
|
+
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Configuration options for KrutAuth
|
|
@@ -121,50 +122,10 @@ declare class KrutAuth {
|
|
|
121
122
|
getSession(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
|
|
122
123
|
}
|
|
123
124
|
|
|
124
|
-
/**
|
|
125
|
-
* API Key Validation Module
|
|
126
|
-
*
|
|
127
|
-
* Centralized API key validation for @krutai/auth.
|
|
128
|
-
* This is bundled directly into @krutai/auth so no separate `krutai` package install is needed.
|
|
129
|
-
*/
|
|
130
|
-
/**
|
|
131
|
-
* Custom error for API key validation failures
|
|
132
|
-
*/
|
|
133
|
-
declare class ApiKeyValidationError extends Error {
|
|
134
|
-
constructor(message: string);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Validates the format of an API key
|
|
138
|
-
* @param apiKey - The API key to validate
|
|
139
|
-
* @throws {ApiKeyValidationError} If the API key format is invalid
|
|
140
|
-
*/
|
|
141
|
-
declare function validateApiKeyFormat(apiKey: string): void;
|
|
142
|
-
/**
|
|
143
|
-
* Validates an API key with the KrutAI service
|
|
144
|
-
* @param apiKey - The API key to validate
|
|
145
|
-
* @returns Promise that resolves to true if valid, false otherwise
|
|
146
|
-
*/
|
|
147
|
-
declare function validateApiKeyWithService(apiKey: string): Promise<boolean>;
|
|
148
|
-
/**
|
|
149
|
-
* Creates a validated API key checker function
|
|
150
|
-
* @param apiKey - The API key to validate
|
|
151
|
-
* @returns A function that checks if the API key is valid
|
|
152
|
-
*/
|
|
153
|
-
declare function createApiKeyChecker(apiKey: string): {
|
|
154
|
-
/**
|
|
155
|
-
* Validates the API key (cached after first call)
|
|
156
|
-
*/
|
|
157
|
-
validate(): Promise<boolean>;
|
|
158
|
-
/**
|
|
159
|
-
* Resets the validation cache
|
|
160
|
-
*/
|
|
161
|
-
reset(): void;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
125
|
/**
|
|
165
126
|
* @krutai/auth - Authentication package for KrutAI
|
|
166
127
|
*
|
|
167
|
-
*
|
|
128
|
+
* Requires `krutai` as a peer dependency (installed automatically).
|
|
168
129
|
*
|
|
169
130
|
* @example Server-side (Next.js API route / server component)
|
|
170
131
|
* ```typescript
|
|
@@ -189,4 +150,4 @@ declare function createApiKeyChecker(apiKey: string): {
|
|
|
189
150
|
|
|
190
151
|
declare const VERSION = "0.1.2";
|
|
191
152
|
|
|
192
|
-
export {
|
|
153
|
+
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
|