@krutai/auth 0.1.1 → 0.1.3
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/README.md +60 -139
- package/dist/index.d.mts +62 -6
- package/dist/index.d.ts +62 -6
- package/dist/index.js +51570 -27
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +51564 -11
- 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/dist/react.d.mts +1 -0
- package/dist/react.d.ts +1 -0
- package/dist/react.js +1648 -0
- package/dist/react.js.map +1 -0
- package/dist/react.mjs +1645 -0
- package/dist/react.mjs.map +1 -0
- package/package.json +17 -5
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
|
|
8
|
+
- 🚀 **Better Auth Integration** — Built on top of Better Auth
|
|
9
|
+
- 📦 **Self-Contained** — No extra installs needed; `better-auth` and `better-sqlite3` are included automatically
|
|
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,110 @@ Authentication package for KrutAI powered by [Krut AI](https://www.krut.ai/).
|
|
|
16
17
|
npm install @krutai/auth
|
|
17
18
|
```
|
|
18
19
|
|
|
19
|
-
> **Note:** Installing `@krutai/auth` automatically installs
|
|
20
|
+
> **Note:** Installing `@krutai/auth` automatically installs `better-sqlite3` (the default database adapter) and bundles `better-auth` — 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
|
-
});
|
|
40
|
+
// app/api/auth/[...all]/route.ts
|
|
41
|
+
import { auth } from "@/lib/auth";
|
|
42
|
+
import { toNextJsHandler } from "@krutai/auth/next-js";
|
|
58
43
|
|
|
59
|
-
|
|
60
|
-
const betterAuth = auth.getBetterAuth();
|
|
44
|
+
export const { GET, POST } = toNextJsHandler(auth);
|
|
61
45
|
```
|
|
62
46
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
### `KrutAuth`
|
|
66
|
-
|
|
67
|
-
Main authentication client class.
|
|
68
|
-
|
|
69
|
-
#### Constructor
|
|
47
|
+
### Client-side (React / Next.js)
|
|
70
48
|
|
|
71
49
|
```typescript
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
**Parameters:**
|
|
50
|
+
// lib/auth-client.ts
|
|
51
|
+
import { createAuthClient } from "@krutai/auth/react";
|
|
76
52
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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.
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
await auth.initialize();
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
**Returns:** `Promise<void>`
|
|
97
|
-
|
|
98
|
-
**Throws:** `ApiKeyValidationError` if validation fails
|
|
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`, validators |
|
|
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
|
-
##
|
|
182
|
-
|
|
183
|
-
This package wraps [Better Auth](https://www.better-auth.com/) and adds API key validation. You can access the full Better Auth API through the `getBetterAuth()` method:
|
|
184
|
-
|
|
185
|
-
```typescript
|
|
186
|
-
const auth = new KrutAuth({ apiKey: 'your-key' });
|
|
187
|
-
await auth.initialize();
|
|
118
|
+
## Architecture
|
|
188
119
|
|
|
189
|
-
|
|
190
|
-
// Use Better Auth features directly
|
|
191
|
-
```
|
|
120
|
+
`@krutai/auth` is fully self-contained — `better-auth` is bundled into the output and `better-sqlite3` is auto-installed as a dependency. Each `@krutai/*` sub-library independently includes its own API key validation, so you never need to install the parent `krutai` package separately.
|
|
192
121
|
|
|
193
122
|
For Better Auth documentation, visit: https://www.better-auth.com/docs
|
|
194
123
|
|
|
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
124
|
## License
|
|
204
125
|
|
|
205
126
|
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as better_auth from 'better-auth';
|
|
2
2
|
import { BetterAuthOptions, betterAuth } from 'better-auth';
|
|
3
|
-
export { BetterAuthOptions } from 'better-auth';
|
|
4
|
-
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
3
|
+
export { BetterAuthOptions, betterAuth } from 'better-auth';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Configuration options for KrutAuth
|
|
@@ -122,15 +121,72 @@ declare class KrutAuth {
|
|
|
122
121
|
getSession(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
|
|
123
122
|
}
|
|
124
123
|
|
|
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
|
+
|
|
125
164
|
/**
|
|
126
165
|
* @krutai/auth - Authentication package for KrutAI
|
|
127
166
|
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
167
|
+
* Everything is bundled — no need to separately install `krutai` or `better-auth`.
|
|
168
|
+
*
|
|
169
|
+
* @example Server-side (Next.js API route / server component)
|
|
170
|
+
* ```typescript
|
|
171
|
+
* import { betterAuth } from "@krutai/auth";
|
|
172
|
+
*
|
|
173
|
+
* export const auth = betterAuth({ ... });
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* @example Client-side (React / Next.js client component)
|
|
177
|
+
* ```typescript
|
|
178
|
+
* import { createAuthClient } from "@krutai/auth/react";
|
|
179
|
+
*
|
|
180
|
+
* export const authClient = createAuthClient({
|
|
181
|
+
* baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* export const { signIn, signUp, signOut, useSession } = authClient;
|
|
185
|
+
* ```
|
|
130
186
|
*
|
|
131
187
|
* @packageDocumentation
|
|
132
188
|
*/
|
|
133
189
|
|
|
134
|
-
declare const VERSION = "0.1.
|
|
190
|
+
declare const VERSION = "0.1.2";
|
|
135
191
|
|
|
136
|
-
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
|
|
192
|
+
export { ApiKeyValidationError, type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as better_auth from 'better-auth';
|
|
2
2
|
import { BetterAuthOptions, betterAuth } from 'better-auth';
|
|
3
|
-
export { BetterAuthOptions } from 'better-auth';
|
|
4
|
-
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
3
|
+
export { BetterAuthOptions, betterAuth } from 'better-auth';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Configuration options for KrutAuth
|
|
@@ -122,15 +121,72 @@ declare class KrutAuth {
|
|
|
122
121
|
getSession(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
|
|
123
122
|
}
|
|
124
123
|
|
|
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
|
+
|
|
125
164
|
/**
|
|
126
165
|
* @krutai/auth - Authentication package for KrutAI
|
|
127
166
|
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
167
|
+
* Everything is bundled — no need to separately install `krutai` or `better-auth`.
|
|
168
|
+
*
|
|
169
|
+
* @example Server-side (Next.js API route / server component)
|
|
170
|
+
* ```typescript
|
|
171
|
+
* import { betterAuth } from "@krutai/auth";
|
|
172
|
+
*
|
|
173
|
+
* export const auth = betterAuth({ ... });
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* @example Client-side (React / Next.js client component)
|
|
177
|
+
* ```typescript
|
|
178
|
+
* import { createAuthClient } from "@krutai/auth/react";
|
|
179
|
+
*
|
|
180
|
+
* export const authClient = createAuthClient({
|
|
181
|
+
* baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* export const { signIn, signUp, signOut, useSession } = authClient;
|
|
185
|
+
* ```
|
|
130
186
|
*
|
|
131
187
|
* @packageDocumentation
|
|
132
188
|
*/
|
|
133
189
|
|
|
134
|
-
declare const VERSION = "0.1.
|
|
190
|
+
declare const VERSION = "0.1.2";
|
|
135
191
|
|
|
136
|
-
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
|
|
192
|
+
export { ApiKeyValidationError, type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService };
|