@krutai/auth 0.1.2 → 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/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 +11 -2
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'better-auth/next-js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'better-auth/next-js';
|