@krutai/auth 0.2.3 → 0.4.2
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 +184 -114
- package/README.md +154 -74
- package/dist/index.d.mts +214 -84
- package/dist/index.d.ts +214 -84
- package/dist/index.js +152 -64
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +147 -61
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -23
- package/dist/next-js.d.mts +0 -1
- package/dist/next-js.d.ts +0 -1
- package/dist/next-js.js +0 -14
- package/dist/next-js.js.map +0 -1
- package/dist/next-js.mjs +0 -3
- package/dist/next-js.mjs.map +0 -1
- package/dist/react.d.mts +0 -1
- package/dist/react.d.ts +0 -1
- package/dist/react.js +0 -14
- package/dist/react.js.map +0 -1
- package/dist/react.mjs +0 -3
- package/dist/react.mjs.map +0 -1
- package/scripts/migrate.js +0 -53
package/README.md
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
# @krutai/auth
|
|
2
2
|
|
|
3
|
-
Authentication package for KrutAI powered by [Better Auth](https://www.better-auth.com/).
|
|
3
|
+
Authentication package for KrutAI — a fetch-based HTTP client that calls your server's `/lib-auth` routes (powered by [Better Auth](https://www.better-auth.com/) on the server side).
|
|
4
|
+
|
|
5
|
+
> **Architecture Note:** This package is a **pure HTTP client** — it has no local database or Better Auth dependency. All auth logic (including database connections) lives on your server. This package simply makes authenticated HTTP calls to your server's auth routes.
|
|
4
6
|
|
|
5
7
|
## Features
|
|
6
8
|
|
|
7
9
|
- 🔐 **API Key Protection** — Requires a valid KrutAI API key (validated via `krutai`)
|
|
8
|
-
- 🚀 **Better Auth Integration** —
|
|
9
|
-
-
|
|
10
|
-
- 🗄️ **Auto-migrates SQLite** — Database tables are created automatically on install
|
|
11
|
-
- 🎯 **Next.js Ready** — First-class support via `@krutai/auth/next-js`
|
|
10
|
+
- 🚀 **Better Auth Integration** — Calls your server's Better Auth routes
|
|
11
|
+
- 🐘 **PostgreSQL Ready** — Your server can use any Better Auth-supported database (PostgreSQL, MySQL, etc.)
|
|
12
12
|
- ⚡ **Dual Format** — Supports both ESM and CommonJS
|
|
13
13
|
- 🔷 **TypeScript First** — Full type safety and IntelliSense
|
|
14
|
+
- 🌐 **Zero DB Dependencies** — No local database driver needed
|
|
14
15
|
|
|
15
16
|
## Installation
|
|
16
17
|
|
|
@@ -18,135 +19,214 @@ Authentication package for KrutAI powered by [Better Auth](https://www.better-au
|
|
|
18
19
|
npm install @krutai/auth
|
|
19
20
|
```
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
## How It Works
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
Your App
|
|
26
|
+
└── @krutai/auth (HTTP client)
|
|
27
|
+
└── POST /lib-auth/api/auth/sign-up/email ──► Your Server
|
|
28
|
+
└── better-auth
|
|
29
|
+
└── PostgreSQL
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
All database operations (user storage, session management, etc.) happen on your server. This package is just a thin, type-safe HTTP wrapper.
|
|
22
33
|
|
|
23
34
|
## Quick Start
|
|
24
35
|
|
|
25
|
-
### Server-side setup (
|
|
36
|
+
### 1. Server-side setup (PostgreSQL + Better Auth)
|
|
26
37
|
|
|
27
|
-
|
|
28
|
-
// lib/auth.ts
|
|
29
|
-
import { krutAuth } from "@krutai/auth";
|
|
30
|
-
import Database from "better-sqlite3";
|
|
38
|
+
Set up Better Auth on your server with a PostgreSQL database:
|
|
31
39
|
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
```typescript
|
|
41
|
+
// server: lib/auth.ts
|
|
42
|
+
import { betterAuth } from "better-auth";
|
|
43
|
+
import { Pool } from "pg";
|
|
44
|
+
|
|
45
|
+
export const auth = betterAuth({
|
|
46
|
+
database: new Pool({
|
|
47
|
+
connectionString: process.env.DATABASE_URL,
|
|
48
|
+
// e.g. postgresql://user:password@localhost:5432/mydb
|
|
49
|
+
}),
|
|
34
50
|
emailAndPassword: {
|
|
35
51
|
enabled: true,
|
|
36
52
|
},
|
|
37
|
-
|
|
53
|
+
basePath: "/lib-auth",
|
|
54
|
+
baseURL: process.env.BETTER_AUTH_BASE_URL ?? "http://localhost:8000",
|
|
55
|
+
secret: process.env.BETTER_AUTH_SECRET,
|
|
38
56
|
});
|
|
39
57
|
```
|
|
40
58
|
|
|
41
|
-
> **Required
|
|
59
|
+
> **Required env vars on your server:**
|
|
60
|
+
> - `DATABASE_URL` — PostgreSQL connection string
|
|
61
|
+
> - `BETTER_AUTH_BASE_URL` — Your server's base URL (e.g. `http://localhost:8000`)
|
|
62
|
+
> - `BETTER_AUTH_SECRET` — Secret for signing sessions
|
|
42
63
|
|
|
43
|
-
###
|
|
64
|
+
### 2. Mount the auth handler on your server
|
|
44
65
|
|
|
45
66
|
```typescript
|
|
46
|
-
//
|
|
47
|
-
import {
|
|
48
|
-
import {
|
|
67
|
+
// server: routes/auth.ts (Express example)
|
|
68
|
+
import { toNodeHandler } from "better-auth/node";
|
|
69
|
+
import { auth } from "./lib/auth";
|
|
49
70
|
|
|
50
|
-
|
|
71
|
+
app.use("/lib-auth", toNodeHandler(auth));
|
|
51
72
|
```
|
|
52
73
|
|
|
53
|
-
###
|
|
74
|
+
### 3. Use `@krutai/auth` in your client/consumer app
|
|
54
75
|
|
|
55
76
|
```typescript
|
|
56
|
-
|
|
57
|
-
import { createAuthClient } from "@krutai/auth/react";
|
|
77
|
+
import { krutAuth } from "@krutai/auth";
|
|
58
78
|
|
|
59
|
-
|
|
60
|
-
|
|
79
|
+
const auth = krutAuth({
|
|
80
|
+
apiKey: process.env.KRUTAI_API_KEY!,
|
|
81
|
+
serverUrl: "https://your-server.com", // points to the server above
|
|
61
82
|
});
|
|
62
83
|
|
|
63
|
-
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### With API Key Validation (KrutAuth class)
|
|
84
|
+
await auth.initialize(); // validates API key against server
|
|
67
85
|
|
|
68
|
-
|
|
69
|
-
|
|
86
|
+
// Sign up
|
|
87
|
+
const { token, user } = await auth.signUpEmail({
|
|
88
|
+
email: "user@example.com",
|
|
89
|
+
password: "secret123",
|
|
90
|
+
name: "Alice",
|
|
91
|
+
});
|
|
70
92
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
},
|
|
93
|
+
// Sign in
|
|
94
|
+
const result = await auth.signInEmail({
|
|
95
|
+
email: "user@example.com",
|
|
96
|
+
password: "secret123",
|
|
76
97
|
});
|
|
77
98
|
|
|
78
|
-
|
|
79
|
-
|
|
99
|
+
// Get session
|
|
100
|
+
const session = await auth.getSession(result.token);
|
|
80
101
|
|
|
81
|
-
|
|
102
|
+
// Sign out
|
|
103
|
+
await auth.signOut(result.token);
|
|
104
|
+
```
|
|
82
105
|
|
|
83
|
-
|
|
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 |
|
|
106
|
+
## Configuration
|
|
87
107
|
|
|
88
|
-
|
|
108
|
+
```typescript
|
|
109
|
+
const auth = krutAuth({
|
|
110
|
+
apiKey: "krut_...", // Required (or set KRUTAI_API_KEY env var)
|
|
111
|
+
serverUrl: "https://...", // Default: "http://localhost:8000"
|
|
112
|
+
authPrefix: "/lib-auth", // Default: "/lib-auth"
|
|
113
|
+
validateOnInit: true, // Default: true — set false to skip in tests
|
|
114
|
+
});
|
|
115
|
+
```
|
|
89
116
|
|
|
90
|
-
|
|
|
91
|
-
|
|
92
|
-
|
|
|
93
|
-
|
|
|
94
|
-
|
|
|
117
|
+
| Option | Type | Default | Description |
|
|
118
|
+
|---|---|---|---|
|
|
119
|
+
| `apiKey` | `string` | `process.env.KRUTAI_API_KEY` | Your KrutAI API key |
|
|
120
|
+
| `serverUrl` | `string` | `http://localhost:8000` | Base URL of your server |
|
|
121
|
+
| `authPrefix` | `string` | `/lib-auth` | Path prefix for auth routes |
|
|
122
|
+
| `validateOnInit` | `boolean` | `true` | Validate API key on `initialize()` |
|
|
95
123
|
|
|
96
124
|
## API Reference
|
|
97
125
|
|
|
98
|
-
### `krutAuth(
|
|
126
|
+
### `krutAuth(config)` — Factory (recommended)
|
|
99
127
|
|
|
100
|
-
|
|
128
|
+
Creates a `KrutAuth` instance.
|
|
101
129
|
|
|
102
130
|
```typescript
|
|
103
131
|
import { krutAuth } from "@krutai/auth";
|
|
132
|
+
const auth = krutAuth({ apiKey: "...", serverUrl: "https://..." });
|
|
133
|
+
await auth.initialize();
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### `KrutAuth` class — Methods
|
|
104
137
|
|
|
105
|
-
|
|
138
|
+
| Method | HTTP Call | Description |
|
|
139
|
+
|---|---|---|
|
|
140
|
+
| `initialize()` | validates API key | **Must be called before other methods** |
|
|
141
|
+
| `signUpEmail(params)` | `POST /lib-auth/api/auth/sign-up/email` | Register a new user |
|
|
142
|
+
| `signInEmail(params)` | `POST /lib-auth/api/auth/sign-in/email` | Authenticate a user |
|
|
143
|
+
| `getSession(token)` | `GET /lib-auth/api/auth/get-session` | Retrieve session info |
|
|
144
|
+
| `signOut(token)` | `POST /lib-auth/api/auth/sign-out` | Invalidate a session |
|
|
145
|
+
| `request(method, path, body?)` | Any | Generic helper for custom endpoints |
|
|
146
|
+
| `isInitialized()` | — | Returns `boolean` |
|
|
147
|
+
|
|
148
|
+
### Types
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
interface SignUpEmailParams { email: string; password: string; name: string; }
|
|
152
|
+
interface SignInEmailParams { email: string; password: string; }
|
|
153
|
+
|
|
154
|
+
interface AuthResponse { token: string; user: AuthUser; }
|
|
155
|
+
interface AuthSession { user: AuthUser; session: AuthSessionRecord; }
|
|
156
|
+
|
|
157
|
+
interface AuthUser {
|
|
158
|
+
id: string; email: string; name?: string;
|
|
159
|
+
emailVerified: boolean; createdAt: string; updatedAt: string;
|
|
160
|
+
}
|
|
106
161
|
```
|
|
107
162
|
|
|
108
|
-
|
|
163
|
+
## Environment Variables
|
|
109
164
|
|
|
110
|
-
|
|
165
|
+
### Client app (where `@krutai/auth` is used)
|
|
111
166
|
|
|
112
|
-
|
|
|
113
|
-
|
|
114
|
-
| `
|
|
115
|
-
| `betterAuthOptions` | `object` | — | Better Auth configuration |
|
|
116
|
-
| `validateOnInit` | `boolean` | — | Validate API key on init (default: `true`) |
|
|
167
|
+
| Variable | Required | Description |
|
|
168
|
+
|---|---|---|
|
|
169
|
+
| `KRUTAI_API_KEY` | ✅ | Your KrutAI API key |
|
|
117
170
|
|
|
118
|
-
|
|
171
|
+
### Server (where Better Auth + PostgreSQL runs)
|
|
119
172
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
173
|
+
| Variable | Required | Description |
|
|
174
|
+
|---|---|---|
|
|
175
|
+
| `DATABASE_URL` | ✅ | PostgreSQL connection string |
|
|
176
|
+
| `BETTER_AUTH_BASE_URL` | ✅ | Your server's base URL |
|
|
177
|
+
| `BETTER_AUTH_SECRET` | recommended | Secret for signing sessions |
|
|
124
178
|
|
|
125
179
|
## Error Handling
|
|
126
180
|
|
|
127
181
|
```typescript
|
|
128
|
-
import {
|
|
182
|
+
import { krutAuth, KrutAuthKeyValidationError } from "@krutai/auth";
|
|
129
183
|
|
|
130
184
|
try {
|
|
131
|
-
const auth =
|
|
185
|
+
const auth = krutAuth({ apiKey: "invalid-key" });
|
|
132
186
|
await auth.initialize();
|
|
133
|
-
} catch (
|
|
134
|
-
if (
|
|
135
|
-
console.error("API key
|
|
187
|
+
} catch (e) {
|
|
188
|
+
if (e instanceof KrutAuthKeyValidationError) {
|
|
189
|
+
console.error("Invalid API key:", e.message);
|
|
190
|
+
} else {
|
|
191
|
+
console.error("Auth error:", e);
|
|
136
192
|
}
|
|
137
193
|
}
|
|
138
194
|
```
|
|
139
195
|
|
|
196
|
+
## Custom Endpoints
|
|
197
|
+
|
|
198
|
+
Use the `request()` method to call any Better Auth endpoint not covered by the convenience methods:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
const data = await auth.request("POST", "/api/auth/some-custom-endpoint", {
|
|
202
|
+
someParam: "value",
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Skipping Validation in Tests
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
const auth = krutAuth({
|
|
210
|
+
apiKey: "test-api-key",
|
|
211
|
+
serverUrl: "http://localhost:8000",
|
|
212
|
+
validateOnInit: false, // Skip server round-trip in tests
|
|
213
|
+
});
|
|
214
|
+
// No need to call initialize()
|
|
215
|
+
```
|
|
216
|
+
|
|
140
217
|
## Architecture
|
|
141
218
|
|
|
142
219
|
```
|
|
143
|
-
@krutai/auth@0.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
220
|
+
@krutai/auth@0.4.0
|
|
221
|
+
└── dependency: krutai ← API key format validation (also peerDep)
|
|
222
|
+
|
|
223
|
+
Your Server
|
|
224
|
+
├── better-auth ← Auth engine
|
|
225
|
+
└── pg / postgres ← PostgreSQL adapter
|
|
148
226
|
```
|
|
149
227
|
|
|
228
|
+
For Better Auth PostgreSQL setup, see: https://www.better-auth.com/docs/adapters/postgresql
|
|
229
|
+
|
|
150
230
|
For Better Auth documentation, visit: https://www.better-auth.com/docs
|
|
151
231
|
|
|
152
232
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -1,150 +1,280 @@
|
|
|
1
|
-
|
|
2
|
-
import { BetterAuthOptions, Auth } from 'better-auth';
|
|
3
|
-
export { BetterAuthOptions } from 'better-auth';
|
|
4
|
-
export { ApiKeyValidationError, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
1
|
+
export { KrutAIKeyValidationError, validateApiKey, validateApiKeyFormat } from 'krutai';
|
|
5
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Types for @krutai/auth
|
|
5
|
+
*
|
|
6
|
+
* Pure fetch-based auth client — no local better-auth dependency.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Default base URL for the KrutAI server.
|
|
10
|
+
* Used when no serverUrl is provided in the config.
|
|
11
|
+
*/
|
|
12
|
+
declare const DEFAULT_SERVER_URL: "http://localhost:8000";
|
|
13
|
+
/**
|
|
14
|
+
* Default path prefix for the auth routes on the server.
|
|
15
|
+
* The server mounts better-auth under this prefix.
|
|
16
|
+
*/
|
|
17
|
+
declare const DEFAULT_AUTH_PREFIX: "/lib-auth";
|
|
6
18
|
/**
|
|
7
19
|
* Configuration options for KrutAuth
|
|
8
20
|
*/
|
|
9
21
|
interface KrutAuthConfig {
|
|
10
22
|
/**
|
|
11
|
-
* API key
|
|
23
|
+
* KrutAI API key.
|
|
24
|
+
* Validated against the server before use.
|
|
12
25
|
* Optional: defaults to process.env.KRUTAI_API_KEY
|
|
13
26
|
*/
|
|
14
27
|
apiKey?: string;
|
|
15
28
|
/**
|
|
16
|
-
*
|
|
17
|
-
* @see https://www.better-auth.com/docs
|
|
18
|
-
*/
|
|
19
|
-
betterAuthOptions?: Partial<BetterAuthOptions>;
|
|
20
|
-
/**
|
|
21
|
-
* Whether to validate the API key on initialization
|
|
22
|
-
* @default true
|
|
23
|
-
*/
|
|
24
|
-
validateOnInit?: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Base URL of your deployed LangChain backend/validation server
|
|
29
|
+
* Base URL of your deployed KrutAI server.
|
|
27
30
|
* @default "http://localhost:8000"
|
|
31
|
+
* @example "https://krut.ai"
|
|
28
32
|
*/
|
|
29
33
|
serverUrl?: string;
|
|
30
34
|
/**
|
|
31
|
-
*
|
|
35
|
+
* Path prefix for the auth routes on the server.
|
|
36
|
+
* @default "/lib-auth"
|
|
32
37
|
*/
|
|
33
|
-
|
|
38
|
+
authPrefix?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Whether to validate the API key against the server on initialization.
|
|
41
|
+
* Set to false to skip the validation round-trip (e.g. in tests).
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
validateOnInit?: boolean;
|
|
34
45
|
}
|
|
35
46
|
/**
|
|
36
|
-
*
|
|
47
|
+
* Parameters for email/password sign-up
|
|
48
|
+
*/
|
|
49
|
+
interface SignUpEmailParams {
|
|
50
|
+
/** User email */
|
|
51
|
+
email: string;
|
|
52
|
+
/** User password */
|
|
53
|
+
password: string;
|
|
54
|
+
/** Display name */
|
|
55
|
+
name: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Parameters for email/password sign-in
|
|
59
|
+
*/
|
|
60
|
+
interface SignInEmailParams {
|
|
61
|
+
/** User email */
|
|
62
|
+
email: string;
|
|
63
|
+
/** User password */
|
|
64
|
+
password: string;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A user record returned by the auth server
|
|
68
|
+
*/
|
|
69
|
+
interface AuthUser {
|
|
70
|
+
id: string;
|
|
71
|
+
email: string;
|
|
72
|
+
name?: string;
|
|
73
|
+
emailVerified: boolean;
|
|
74
|
+
createdAt: string;
|
|
75
|
+
updatedAt: string;
|
|
76
|
+
[key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A session record returned by the auth server
|
|
80
|
+
*/
|
|
81
|
+
interface AuthSessionRecord {
|
|
82
|
+
id: string;
|
|
83
|
+
userId: string;
|
|
84
|
+
token: string;
|
|
85
|
+
expiresAt: string;
|
|
86
|
+
[key: string]: unknown;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Combined session + user response
|
|
37
90
|
*/
|
|
38
91
|
interface AuthSession {
|
|
39
|
-
user:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
92
|
+
user: AuthUser;
|
|
93
|
+
session: AuthSessionRecord;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Sign-up / sign-in response (contains token + user)
|
|
97
|
+
*/
|
|
98
|
+
interface AuthResponse {
|
|
99
|
+
token: string;
|
|
100
|
+
user: AuthUser;
|
|
101
|
+
[key: string]: unknown;
|
|
50
102
|
}
|
|
51
103
|
|
|
52
104
|
/**
|
|
53
|
-
* KrutAuth -
|
|
105
|
+
* KrutAuth — fetch-based authentication client for KrutAI
|
|
54
106
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
107
|
+
* Calls your deployed server's `/lib-auth` routes for all auth operations.
|
|
108
|
+
* The API key is validated against the server before use.
|
|
57
109
|
*
|
|
58
110
|
* @example
|
|
59
111
|
* ```typescript
|
|
60
112
|
* import { KrutAuth } from '@krutai/auth';
|
|
61
113
|
*
|
|
62
114
|
* const auth = new KrutAuth({
|
|
63
|
-
* apiKey:
|
|
64
|
-
*
|
|
65
|
-
* // Better Auth configuration
|
|
66
|
-
* }
|
|
115
|
+
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
116
|
+
* serverUrl: 'https://krut.ai',
|
|
67
117
|
* });
|
|
68
118
|
*
|
|
69
|
-
*
|
|
70
|
-
* await auth.initialize();
|
|
119
|
+
* await auth.initialize(); // validates key against server
|
|
71
120
|
*
|
|
72
|
-
* //
|
|
73
|
-
* const
|
|
121
|
+
* // Sign up
|
|
122
|
+
* const { token, user } = await auth.signUpEmail({
|
|
123
|
+
* email: 'user@example.com',
|
|
124
|
+
* password: 'secret123',
|
|
125
|
+
* name: 'Alice',
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* // Sign in
|
|
129
|
+
* const result = await auth.signInEmail({
|
|
130
|
+
* email: 'user@example.com',
|
|
131
|
+
* password: 'secret123',
|
|
132
|
+
* });
|
|
133
|
+
*
|
|
134
|
+
* // Get session
|
|
135
|
+
* const session = await auth.getSession(result.token);
|
|
74
136
|
* ```
|
|
75
137
|
*/
|
|
76
138
|
declare class KrutAuth {
|
|
77
|
-
private
|
|
78
|
-
private
|
|
79
|
-
private
|
|
139
|
+
private readonly apiKey;
|
|
140
|
+
private readonly serverUrl;
|
|
141
|
+
private readonly authPrefix;
|
|
142
|
+
private readonly config;
|
|
80
143
|
private initialized;
|
|
81
|
-
/**
|
|
82
|
-
* Creates a new KrutAuth instance
|
|
83
|
-
* @param config - Configuration options
|
|
84
|
-
* @throws {ApiKeyValidationError} If API key is invalid
|
|
85
|
-
*/
|
|
86
144
|
constructor(config: KrutAuthConfig);
|
|
87
145
|
/**
|
|
88
|
-
* Initialize the
|
|
89
|
-
* Validates the API key
|
|
90
|
-
*
|
|
146
|
+
* Initialize the auth client.
|
|
147
|
+
* Validates the API key against the server, then marks client as ready.
|
|
148
|
+
*
|
|
149
|
+
* @throws {KrutAuthKeyValidationError} if the key is rejected or the server is unreachable
|
|
91
150
|
*/
|
|
92
151
|
initialize(): Promise<void>;
|
|
93
152
|
/**
|
|
94
|
-
*
|
|
95
|
-
* @private
|
|
153
|
+
* Returns whether the client has been initialized.
|
|
96
154
|
*/
|
|
97
|
-
|
|
155
|
+
isInitialized(): boolean;
|
|
156
|
+
private assertInitialized;
|
|
157
|
+
/** Common request headers sent to the server on every auth call. */
|
|
158
|
+
private authHeaders;
|
|
98
159
|
/**
|
|
99
|
-
*
|
|
100
|
-
* @
|
|
160
|
+
* Build the full URL for an auth endpoint.
|
|
161
|
+
* @param path - The better-auth sub-path, e.g. `/api/auth/sign-up/email`
|
|
101
162
|
*/
|
|
102
|
-
|
|
163
|
+
private url;
|
|
103
164
|
/**
|
|
104
|
-
*
|
|
165
|
+
* Generic request helper for any better-auth endpoint.
|
|
166
|
+
*
|
|
167
|
+
* Use this to call endpoints not covered by the convenience methods.
|
|
168
|
+
*
|
|
169
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
170
|
+
* @param path - The better-auth endpoint path (e.g. `/api/auth/sign-up/email`)
|
|
171
|
+
* @param body - Optional JSON body
|
|
172
|
+
* @returns The parsed JSON response
|
|
105
173
|
*/
|
|
106
|
-
|
|
174
|
+
request<T = unknown>(method: string, path: string, body?: Record<string, unknown> | object): Promise<T>;
|
|
107
175
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
176
|
+
* Sign up a new user with email and password.
|
|
177
|
+
*
|
|
178
|
+
* Calls: POST {serverUrl}/lib-auth/api/auth/sign-up/email
|
|
179
|
+
*
|
|
180
|
+
* @param params - Sign-up parameters (email, password, name)
|
|
181
|
+
* @returns The auth response containing token and user
|
|
110
182
|
*/
|
|
111
|
-
|
|
183
|
+
signUpEmail(params: SignUpEmailParams): Promise<AuthResponse>;
|
|
112
184
|
/**
|
|
113
|
-
* Sign in
|
|
114
|
-
*
|
|
115
|
-
*
|
|
185
|
+
* Sign in with email and password.
|
|
186
|
+
*
|
|
187
|
+
* Calls: POST {serverUrl}/lib-auth/api/auth/sign-in/email
|
|
188
|
+
*
|
|
189
|
+
* @param params - Sign-in parameters (email, password)
|
|
190
|
+
* @returns The auth response containing token and user
|
|
116
191
|
*/
|
|
117
|
-
|
|
192
|
+
signInEmail(params: SignInEmailParams): Promise<AuthResponse>;
|
|
118
193
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
194
|
+
* Get the current session for a user.
|
|
195
|
+
*
|
|
196
|
+
* Calls: GET {serverUrl}/lib-auth/api/auth/get-session
|
|
197
|
+
*
|
|
198
|
+
* @param sessionToken - The session token (Bearer token from sign-in)
|
|
199
|
+
* @returns The session containing user and session data
|
|
121
200
|
*/
|
|
122
|
-
|
|
201
|
+
getSession(sessionToken: string): Promise<AuthSession>;
|
|
123
202
|
/**
|
|
124
|
-
*
|
|
125
|
-
*
|
|
203
|
+
* Sign out the current user.
|
|
204
|
+
*
|
|
205
|
+
* Calls: POST {serverUrl}/lib-auth/api/auth/sign-out
|
|
206
|
+
*
|
|
207
|
+
* @param sessionToken - The session token to invalidate
|
|
126
208
|
*/
|
|
127
|
-
|
|
209
|
+
signOut(sessionToken: string): Promise<void>;
|
|
128
210
|
}
|
|
129
211
|
|
|
130
212
|
/**
|
|
131
|
-
*
|
|
213
|
+
* @krutai/auth — Authentication package for KrutAI
|
|
132
214
|
*
|
|
133
|
-
*
|
|
215
|
+
* A fetch-based wrapper that calls your deployed server's `/lib-auth` routes.
|
|
216
|
+
* The user's API key is validated against the server before any auth call is made.
|
|
134
217
|
*
|
|
135
|
-
* @example
|
|
218
|
+
* @example Basic usage
|
|
136
219
|
* ```typescript
|
|
137
|
-
* import { krutAuth } from
|
|
138
|
-
* import Database from "better-sqlite3";
|
|
220
|
+
* import { krutAuth } from '@krutai/auth';
|
|
139
221
|
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
222
|
+
* const auth = krutAuth({
|
|
223
|
+
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
224
|
+
* serverUrl: 'https://krut.ai',
|
|
225
|
+
* });
|
|
226
|
+
*
|
|
227
|
+
* await auth.initialize(); // validates key with server
|
|
228
|
+
*
|
|
229
|
+
* const { token, user } = await auth.signUpEmail({
|
|
230
|
+
* email: 'user@example.com',
|
|
231
|
+
* password: 'secret123',
|
|
232
|
+
* name: 'Alice',
|
|
143
233
|
* });
|
|
144
234
|
* ```
|
|
235
|
+
*
|
|
236
|
+
* @example Sign in
|
|
237
|
+
* ```typescript
|
|
238
|
+
* const auth = krutAuth({
|
|
239
|
+
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
240
|
+
* serverUrl: 'https://krut.ai',
|
|
241
|
+
* });
|
|
242
|
+
* await auth.initialize();
|
|
243
|
+
*
|
|
244
|
+
* const { token, user } = await auth.signInEmail({
|
|
245
|
+
* email: 'user@example.com',
|
|
246
|
+
* password: 'secret123',
|
|
247
|
+
* });
|
|
248
|
+
* ```
|
|
249
|
+
*
|
|
250
|
+
* @packageDocumentation
|
|
145
251
|
*/
|
|
146
|
-
declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
|
|
147
252
|
|
|
148
|
-
|
|
253
|
+
/**
|
|
254
|
+
* krutAuth — convenience factory.
|
|
255
|
+
*
|
|
256
|
+
* Creates a `KrutAuth` instance configured to call your server's `/lib-auth` routes.
|
|
257
|
+
*
|
|
258
|
+
* @param config - Auth configuration (`apiKey` and `serverUrl` are required)
|
|
259
|
+
* @returns A `KrutAuth` instance — call `.initialize()` before use
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* import { krutAuth } from '@krutai/auth';
|
|
264
|
+
*
|
|
265
|
+
* const auth = krutAuth({
|
|
266
|
+
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
267
|
+
* serverUrl: 'https://krut.ai',
|
|
268
|
+
* });
|
|
269
|
+
*
|
|
270
|
+
* await auth.initialize();
|
|
271
|
+
* const { token, user } = await auth.signInEmail({
|
|
272
|
+
* email: 'user@example.com',
|
|
273
|
+
* password: 'secret123',
|
|
274
|
+
* });
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
declare function krutAuth(config: KrutAuthConfig): KrutAuth;
|
|
278
|
+
declare const VERSION = "0.4.0";
|
|
149
279
|
|
|
150
|
-
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
|
|
280
|
+
export { type AuthResponse, type AuthSession, type AuthSessionRecord, type AuthUser, DEFAULT_AUTH_PREFIX, DEFAULT_SERVER_URL, KrutAuth, type KrutAuthConfig, type SignInEmailParams, type SignUpEmailParams, VERSION, krutAuth };
|