@krutai/auth 0.1.3 → 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 +14 -5
- 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/package.json +5 -2
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
|
@@ -4,9 +4,9 @@ Authentication package for KrutAI powered by [Better Auth](https://www.better-au
|
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- 🔐 **API Key Protection** — Requires a valid KrutAI API key
|
|
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 `krutai`** — The core `krutai` package is installed automatically as a peer dependency
|
|
10
10
|
- 🎯 **Next.js Ready** — First-class support via `@krutai/auth/next-js`
|
|
11
11
|
- ⚡ **Dual Format** — Supports both ESM and CommonJS
|
|
12
12
|
- 🔷 **TypeScript First** — Full type safety and IntelliSense
|
|
@@ -17,7 +17,7 @@ Authentication package for KrutAI powered by [Better Auth](https://www.better-au
|
|
|
17
17
|
npm install @krutai/auth
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
> **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.
|
|
21
21
|
|
|
22
22
|
## Quick Start
|
|
23
23
|
|
|
@@ -76,7 +76,7 @@ await auth.initialize();
|
|
|
76
76
|
|
|
77
77
|
| Import path | What it provides |
|
|
78
78
|
|---|---|
|
|
79
|
-
| `@krutai/auth` | `betterAuth`, `KrutAuth`,
|
|
79
|
+
| `@krutai/auth` | `betterAuth`, `KrutAuth`, validator re-exports from `krutai` |
|
|
80
80
|
| `@krutai/auth/react` | `createAuthClient`, `useSession`, etc. |
|
|
81
81
|
| `@krutai/auth/next-js` | `toNextJsHandler` |
|
|
82
82
|
|
|
@@ -115,9 +115,18 @@ try {
|
|
|
115
115
|
}
|
|
116
116
|
```
|
|
117
117
|
|
|
118
|
+
> `ApiKeyValidationError` is re-exported from `krutai` — the single source of truth for API validation across the KrutAI ecosystem.
|
|
119
|
+
|
|
118
120
|
## Architecture
|
|
119
121
|
|
|
120
|
-
`@krutai/auth`
|
|
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
|
+
```
|
|
125
|
+
@krutai/auth@0.1.4
|
|
126
|
+
├── peerDependency: krutai >=0.1.2 (auto-installed)
|
|
127
|
+
├── dependency: better-sqlite3
|
|
128
|
+
└── bundled: better-auth
|
|
129
|
+
```
|
|
121
130
|
|
|
122
131
|
For Better Auth documentation, visit: https://www.better-auth.com/docs
|
|
123
132
|
|
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 };
|
package/dist/index.d.ts
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var krutai = require('krutai');
|
|
4
|
+
|
|
3
5
|
var __defProp = Object.defineProperty;
|
|
4
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
7
|
var __esm = (fn, res) => function __init() {
|
|
@@ -51491,64 +51493,6 @@ var betterAuth = (options) => {
|
|
|
51491
51493
|
init_env();
|
|
51492
51494
|
init_error();
|
|
51493
51495
|
init_utils();
|
|
51494
|
-
|
|
51495
|
-
// src/validator.ts
|
|
51496
|
-
var ApiKeyValidationError = class extends Error {
|
|
51497
|
-
constructor(message2) {
|
|
51498
|
-
super(message2);
|
|
51499
|
-
this.name = "ApiKeyValidationError";
|
|
51500
|
-
}
|
|
51501
|
-
};
|
|
51502
|
-
function validateApiKeyFormat(apiKey) {
|
|
51503
|
-
if (!apiKey || typeof apiKey !== "string") {
|
|
51504
|
-
throw new ApiKeyValidationError("API key must be a non-empty string");
|
|
51505
|
-
}
|
|
51506
|
-
if (apiKey.trim().length === 0) {
|
|
51507
|
-
throw new ApiKeyValidationError("API key cannot be empty or whitespace");
|
|
51508
|
-
}
|
|
51509
|
-
if (apiKey.length < 10) {
|
|
51510
|
-
throw new ApiKeyValidationError("API key must be at least 10 characters long");
|
|
51511
|
-
}
|
|
51512
|
-
}
|
|
51513
|
-
async function validateApiKeyWithService(apiKey) {
|
|
51514
|
-
validateApiKeyFormat(apiKey);
|
|
51515
|
-
try {
|
|
51516
|
-
return true;
|
|
51517
|
-
} catch (error49) {
|
|
51518
|
-
throw new ApiKeyValidationError(
|
|
51519
|
-
`Failed to validate API key: ${error49 instanceof Error ? error49.message : "Unknown error"}`
|
|
51520
|
-
);
|
|
51521
|
-
}
|
|
51522
|
-
}
|
|
51523
|
-
function createApiKeyChecker(apiKey) {
|
|
51524
|
-
let isValid = null;
|
|
51525
|
-
let validationPromise = null;
|
|
51526
|
-
return {
|
|
51527
|
-
/**
|
|
51528
|
-
* Validates the API key (cached after first call)
|
|
51529
|
-
*/
|
|
51530
|
-
async validate() {
|
|
51531
|
-
if (isValid !== null) {
|
|
51532
|
-
return isValid;
|
|
51533
|
-
}
|
|
51534
|
-
if (validationPromise) {
|
|
51535
|
-
return validationPromise;
|
|
51536
|
-
}
|
|
51537
|
-
validationPromise = validateApiKeyWithService(apiKey);
|
|
51538
|
-
isValid = await validationPromise;
|
|
51539
|
-
return isValid;
|
|
51540
|
-
},
|
|
51541
|
-
/**
|
|
51542
|
-
* Resets the validation cache
|
|
51543
|
-
*/
|
|
51544
|
-
reset() {
|
|
51545
|
-
isValid = null;
|
|
51546
|
-
validationPromise = null;
|
|
51547
|
-
}
|
|
51548
|
-
};
|
|
51549
|
-
}
|
|
51550
|
-
|
|
51551
|
-
// src/client.ts
|
|
51552
51496
|
var KrutAuth = class {
|
|
51553
51497
|
/**
|
|
51554
51498
|
* Creates a new KrutAuth instance
|
|
@@ -51557,7 +51501,7 @@ var KrutAuth = class {
|
|
|
51557
51501
|
*/
|
|
51558
51502
|
constructor(config3) {
|
|
51559
51503
|
this.config = config3;
|
|
51560
|
-
validateApiKeyFormat(config3.apiKey);
|
|
51504
|
+
krutai.validateApiKeyFormat(config3.apiKey);
|
|
51561
51505
|
this.apiKey = config3.apiKey;
|
|
51562
51506
|
if (config3.validateOnInit === false) {
|
|
51563
51507
|
this.initializeBetterAuth();
|
|
@@ -51576,7 +51520,7 @@ var KrutAuth = class {
|
|
|
51576
51520
|
return;
|
|
51577
51521
|
}
|
|
51578
51522
|
if (this.config.validateOnInit !== false) {
|
|
51579
|
-
await validateApiKeyWithService(this.apiKey);
|
|
51523
|
+
await krutai.validateApiKeyWithService(this.apiKey);
|
|
51580
51524
|
}
|
|
51581
51525
|
this.initializeBetterAuth();
|
|
51582
51526
|
this.initialized = true;
|
|
@@ -51641,8 +51585,6 @@ var KrutAuth = class {
|
|
|
51641
51585
|
return auth;
|
|
51642
51586
|
}
|
|
51643
51587
|
};
|
|
51644
|
-
|
|
51645
|
-
// src/index.ts
|
|
51646
51588
|
var VERSION = "0.1.2";
|
|
51647
51589
|
/*! Bundled license information:
|
|
51648
51590
|
|
|
@@ -51653,12 +51595,24 @@ var VERSION = "0.1.2";
|
|
|
51653
51595
|
(*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
|
|
51654
51596
|
*/
|
|
51655
51597
|
|
|
51656
|
-
exports
|
|
51598
|
+
Object.defineProperty(exports, "ApiKeyValidationError", {
|
|
51599
|
+
enumerable: true,
|
|
51600
|
+
get: function () { return krutai.ApiKeyValidationError; }
|
|
51601
|
+
});
|
|
51602
|
+
Object.defineProperty(exports, "createApiKeyChecker", {
|
|
51603
|
+
enumerable: true,
|
|
51604
|
+
get: function () { return krutai.createApiKeyChecker; }
|
|
51605
|
+
});
|
|
51606
|
+
Object.defineProperty(exports, "validateApiKeyFormat", {
|
|
51607
|
+
enumerable: true,
|
|
51608
|
+
get: function () { return krutai.validateApiKeyFormat; }
|
|
51609
|
+
});
|
|
51610
|
+
Object.defineProperty(exports, "validateApiKeyWithService", {
|
|
51611
|
+
enumerable: true,
|
|
51612
|
+
get: function () { return krutai.validateApiKeyWithService; }
|
|
51613
|
+
});
|
|
51657
51614
|
exports.KrutAuth = KrutAuth;
|
|
51658
51615
|
exports.VERSION = VERSION;
|
|
51659
51616
|
exports.betterAuth = betterAuth;
|
|
51660
|
-
exports.createApiKeyChecker = createApiKeyChecker;
|
|
51661
|
-
exports.validateApiKeyFormat = validateApiKeyFormat;
|
|
51662
|
-
exports.validateApiKeyWithService = validateApiKeyWithService;
|
|
51663
51617
|
//# sourceMappingURL=index.js.map
|
|
51664
51618
|
//# sourceMappingURL=index.js.map
|