@krutai/auth 0.1.6 → 0.1.7
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 +86 -85
- package/README.md +35 -19
- package/dist/index.d.mts +12 -23
- package/dist/index.d.ts +12 -23
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/AI_REFERENCE.md
CHANGED
|
@@ -3,32 +3,33 @@
|
|
|
3
3
|
## Package Overview
|
|
4
4
|
|
|
5
5
|
- **Name**: `@krutai/auth`
|
|
6
|
-
- **Version**: `0.1.
|
|
7
|
-
- **Purpose**: Authentication package for KrutAI — wraps Better Auth with
|
|
6
|
+
- **Version**: `0.1.7`
|
|
7
|
+
- **Purpose**: Authentication package for KrutAI — wraps Better Auth with a `krutAuth` function and optional API key validation via `KrutAuth` class
|
|
8
8
|
- **Entry**: `src/index.ts` → `dist/index.{js,mjs,d.ts}`
|
|
9
|
-
- **Build**: `tsup` (CJS + ESM,
|
|
9
|
+
- **Build**: `tsup` (CJS + ESM, all deps external)
|
|
10
10
|
|
|
11
11
|
## Dependency Architecture
|
|
12
12
|
|
|
13
13
|
```
|
|
14
|
-
@krutai/auth@0.1.
|
|
15
|
-
├──
|
|
16
|
-
├── dependency:
|
|
17
|
-
|
|
14
|
+
@krutai/auth@0.1.7
|
|
15
|
+
├── dependency: krutai ← API key validation (also peerDep)
|
|
16
|
+
├── dependency: better-auth ← auth engine (external in tsup)
|
|
17
|
+
├── dependency: better-sqlite3 ← default SQLite adapter
|
|
18
|
+
└── dependency: @types/better-sqlite3
|
|
18
19
|
```
|
|
19
20
|
|
|
20
|
-
> **Important for AI**:
|
|
21
|
+
> **Important for AI**: Do NOT bundle `better-auth` or `krutai` inline (no `noExternal`). They are real dependencies and must stay external in tsup.
|
|
21
22
|
|
|
22
23
|
## File Structure
|
|
23
24
|
|
|
24
25
|
```
|
|
25
26
|
packages/auth/
|
|
26
27
|
├── src/
|
|
27
|
-
│ ├── index.ts #
|
|
28
|
-
│ ├── client.ts # KrutAuth class
|
|
28
|
+
│ ├── index.ts # Exports krutAuth function + KrutAuth class + validator re-exports
|
|
29
|
+
│ ├── client.ts # KrutAuth class (API-key-protected wrapper)
|
|
29
30
|
│ ├── types.ts # KrutAuthConfig, AuthSession, BetterAuthOptions
|
|
30
|
-
│ ├── react.ts #
|
|
31
|
-
│ └── next-js.ts #
|
|
31
|
+
│ ├── react.ts # re-exports better-auth/react (createAuthClient, hooks)
|
|
32
|
+
│ └── next-js.ts # re-exports better-auth/next-js (toNextJsHandler)
|
|
32
33
|
├── package.json
|
|
33
34
|
├── tsconfig.json
|
|
34
35
|
└── tsup.config.ts
|
|
@@ -38,28 +39,55 @@ packages/auth/
|
|
|
38
39
|
|
|
39
40
|
| Import | File | Purpose |
|
|
40
41
|
|---|---|---|
|
|
41
|
-
| `@krutai/auth` | `dist/index.js` |
|
|
42
|
-
| `@krutai/auth/react` | `dist/react.js` |
|
|
43
|
-
| `@krutai/auth/next-js` | `dist/next-js.js` |
|
|
42
|
+
| `@krutai/auth` | `dist/index.js` | `krutAuth`, `KrutAuth`, validator re-exports |
|
|
43
|
+
| `@krutai/auth/react` | `dist/react.js` | `createAuthClient`, hooks |
|
|
44
|
+
| `@krutai/auth/next-js` | `dist/next-js.js` | `toNextJsHandler` |
|
|
44
45
|
|
|
45
46
|
## Main Exports
|
|
46
47
|
|
|
47
|
-
###
|
|
48
|
+
### `krutAuth(options)` ← PRIMARY API
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
Main authentication client.
|
|
50
|
+
Drop-in replacement for `betterAuth`. Users should always use this.
|
|
51
51
|
|
|
52
|
-
**Constructor:**
|
|
53
52
|
```typescript
|
|
54
|
-
|
|
53
|
+
import { krutAuth } from "@krutai/auth";
|
|
54
|
+
import Database from "better-sqlite3";
|
|
55
|
+
|
|
56
|
+
export const auth = krutAuth({
|
|
57
|
+
database: new Database("./sqlite.db"),
|
|
58
|
+
emailAndPassword: { enabled: true },
|
|
59
|
+
user: {
|
|
60
|
+
additionalFields: {
|
|
61
|
+
name: { type: "string", required: true },
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `KrutAuth` class ← API-KEY-PROTECTED WRAPPER
|
|
68
|
+
|
|
69
|
+
For when you need API key validation before initializing auth.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { KrutAuth } from "@krutai/auth";
|
|
73
|
+
|
|
74
|
+
const auth = new KrutAuth({
|
|
75
|
+
apiKey: process.env.KRUTAI_API_KEY!,
|
|
76
|
+
betterAuthOptions: {
|
|
77
|
+
database: { provider: 'postgres', url: process.env.DATABASE_URL },
|
|
78
|
+
emailAndPassword: { enabled: true },
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
await auth.initialize();
|
|
83
|
+
const betterAuthInstance = auth.getBetterAuth();
|
|
55
84
|
```
|
|
56
85
|
|
|
57
86
|
**Methods:**
|
|
58
87
|
- `initialize(): Promise<void>` — validates API key + initializes Better Auth
|
|
59
|
-
- `getBetterAuth()` — returns the Better Auth instance
|
|
88
|
+
- `getBetterAuth(): Auth` — returns the Better Auth `Auth` instance
|
|
60
89
|
- `isInitialized(): boolean`
|
|
61
90
|
- `getApiKey(): string`
|
|
62
|
-
- `signIn()`, `signOut()`, `getSession()` — convenience wrappers
|
|
63
91
|
|
|
64
92
|
### Types
|
|
65
93
|
|
|
@@ -73,14 +101,6 @@ interface KrutAuthConfig {
|
|
|
73
101
|
}
|
|
74
102
|
```
|
|
75
103
|
|
|
76
|
-
#### `AuthSession`
|
|
77
|
-
```typescript
|
|
78
|
-
interface AuthSession {
|
|
79
|
-
user: { id: string; email: string; name?: string; [key: string]: unknown };
|
|
80
|
-
session: { id: string; expiresAt: Date; [key: string]: unknown };
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
104
|
### Validator Re-exports (from `krutai`)
|
|
85
105
|
|
|
86
106
|
```typescript
|
|
@@ -88,95 +108,76 @@ interface AuthSession {
|
|
|
88
108
|
export { validateApiKeyFormat, validateApiKeyWithService, createApiKeyChecker, ApiKeyValidationError } from 'krutai';
|
|
89
109
|
```
|
|
90
110
|
|
|
91
|
-
|
|
111
|
+
## Usage Examples
|
|
92
112
|
|
|
113
|
+
### Example 1: Standard Server Setup (recommended)
|
|
93
114
|
```typescript
|
|
94
|
-
|
|
95
|
-
|
|
115
|
+
import { krutAuth } from "@krutai/auth";
|
|
116
|
+
import Database from "better-sqlite3";
|
|
96
117
|
|
|
97
|
-
|
|
118
|
+
export const auth = krutAuth({
|
|
119
|
+
database: new Database("./sqlite.db"),
|
|
120
|
+
emailAndPassword: { enabled: true },
|
|
121
|
+
});
|
|
122
|
+
```
|
|
98
123
|
|
|
99
|
-
### Example
|
|
124
|
+
### Example 2: Next.js Route Handler
|
|
100
125
|
```typescript
|
|
101
|
-
|
|
126
|
+
// app/api/auth/[...all]/route.ts
|
|
127
|
+
import { auth } from "@/lib/auth";
|
|
128
|
+
import { toNextJsHandler } from "@krutai/auth/next-js";
|
|
102
129
|
|
|
103
|
-
export const
|
|
104
|
-
database: { /* config */ },
|
|
105
|
-
});
|
|
130
|
+
export const { GET, POST } = toNextJsHandler(auth);
|
|
106
131
|
```
|
|
107
132
|
|
|
108
|
-
### Example
|
|
133
|
+
### Example 3: React Client
|
|
109
134
|
```typescript
|
|
110
|
-
import {
|
|
135
|
+
import { createAuthClient } from "@krutai/auth/react";
|
|
111
136
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
betterAuthOptions: {
|
|
115
|
-
database: { provider: 'postgres', url: process.env.DATABASE_URL },
|
|
116
|
-
emailAndPassword: { enabled: true },
|
|
117
|
-
},
|
|
137
|
+
export const authClient = createAuthClient({
|
|
138
|
+
baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
|
|
118
139
|
});
|
|
119
|
-
|
|
120
|
-
await auth.initialize();
|
|
121
|
-
const betterAuth = auth.getBetterAuth();
|
|
140
|
+
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
122
141
|
```
|
|
123
142
|
|
|
124
|
-
### Example
|
|
143
|
+
### Example 4: KrutAuth with API Key Validation
|
|
125
144
|
```typescript
|
|
145
|
+
import { KrutAuth } from "@krutai/auth";
|
|
146
|
+
|
|
126
147
|
const auth = new KrutAuth({
|
|
127
148
|
apiKey: process.env.KRUTAI_API_KEY!,
|
|
128
|
-
|
|
129
|
-
betterAuthOptions: { /* config */ },
|
|
149
|
+
betterAuthOptions: { emailAndPassword: { enabled: true } },
|
|
130
150
|
});
|
|
131
|
-
|
|
132
|
-
const betterAuth = auth.getBetterAuth();
|
|
151
|
+
await auth.initialize();
|
|
133
152
|
```
|
|
134
153
|
|
|
135
|
-
### Example
|
|
154
|
+
### Example 5: Error Handling
|
|
136
155
|
```typescript
|
|
137
|
-
import { KrutAuth, ApiKeyValidationError } from
|
|
156
|
+
import { KrutAuth, ApiKeyValidationError } from "@krutai/auth";
|
|
138
157
|
|
|
139
158
|
try {
|
|
140
|
-
const auth = new KrutAuth({ apiKey:
|
|
159
|
+
const auth = new KrutAuth({ apiKey: "bad" });
|
|
141
160
|
await auth.initialize();
|
|
142
161
|
} catch (e) {
|
|
143
162
|
if (e instanceof ApiKeyValidationError) {
|
|
144
|
-
console.error(
|
|
163
|
+
console.error("Invalid API key:", e.message);
|
|
145
164
|
}
|
|
146
165
|
}
|
|
147
166
|
```
|
|
148
167
|
|
|
149
|
-
### Example 5: Next.js Route Handler
|
|
150
|
-
```typescript
|
|
151
|
-
// app/api/auth/[...all]/route.ts
|
|
152
|
-
import { auth } from '@/lib/auth';
|
|
153
|
-
import { toNextJsHandler } from '@krutai/auth/next-js';
|
|
154
|
-
|
|
155
|
-
export const { GET, POST } = toNextJsHandler(auth);
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### Example 6: React Client
|
|
159
|
-
```typescript
|
|
160
|
-
import { createAuthClient } from '@krutai/auth/react';
|
|
161
|
-
|
|
162
|
-
export const authClient = createAuthClient({
|
|
163
|
-
baseURL: process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000',
|
|
164
|
-
});
|
|
165
|
-
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
166
|
-
```
|
|
167
|
-
|
|
168
168
|
## tsup Configuration Notes
|
|
169
169
|
|
|
170
|
-
- `better-auth` →
|
|
171
|
-
- `krutai` → external (peer dep, NOT bundled)
|
|
172
|
-
- `
|
|
170
|
+
- `better-auth` → **external** (real dependency, NOT bundled)
|
|
171
|
+
- `krutai` → **external** (peer dep, NOT bundled)
|
|
172
|
+
- `better-sqlite3` → **external** (real dependency)
|
|
173
|
+
- `react`, `react-dom`, `next` → external
|
|
173
174
|
|
|
174
175
|
## Important Notes
|
|
175
176
|
|
|
176
|
-
1. **
|
|
177
|
-
2.
|
|
178
|
-
3.
|
|
179
|
-
4.
|
|
177
|
+
1. **Use `krutAuth` not `betterAuth`**: The public API is `krutAuth`. `betterAuth` is an internal implementation detail
|
|
178
|
+
2. **Validator lives in `krutai`**: Never add a local `validator.ts` — import from `krutai`
|
|
179
|
+
3. **No `noExternal` for `better-auth` or `krutai`**: They must stay external in tsup
|
|
180
|
+
4. **`getBetterAuth()` returns `Auth`**: Uses the `Auth` type from `better-auth`, not `ReturnType<typeof betterAuth>`
|
|
180
181
|
|
|
181
182
|
## Related Packages
|
|
182
183
|
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Authentication package for KrutAI powered by [Better Auth](https://www.better-au
|
|
|
6
6
|
|
|
7
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
|
-
- 📦 **Auto-installs
|
|
9
|
+
- 📦 **Auto-installs everything** — `krutai`, `better-auth`, `better-sqlite3` install automatically
|
|
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:** `krutai`
|
|
20
|
+
> **Note:** `krutai`, `better-auth`, `better-sqlite3`, and `@types/better-sqlite3` are all installed automatically.
|
|
21
21
|
|
|
22
22
|
## Quick Start
|
|
23
23
|
|
|
@@ -25,11 +25,21 @@ npm install @krutai/auth
|
|
|
25
25
|
|
|
26
26
|
```typescript
|
|
27
27
|
// lib/auth.ts
|
|
28
|
-
import {
|
|
28
|
+
import { krutAuth } from "@krutai/auth";
|
|
29
|
+
import Database from "better-sqlite3";
|
|
29
30
|
|
|
30
|
-
export const auth =
|
|
31
|
-
database:
|
|
32
|
-
|
|
31
|
+
export const auth = krutAuth({
|
|
32
|
+
database: new Database("./sqlite.db"),
|
|
33
|
+
emailAndPassword: {
|
|
34
|
+
enabled: true,
|
|
35
|
+
},
|
|
36
|
+
user: {
|
|
37
|
+
additionalFields: {
|
|
38
|
+
name: {
|
|
39
|
+
type: "string",
|
|
40
|
+
required: true,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
33
43
|
},
|
|
34
44
|
});
|
|
35
45
|
```
|
|
@@ -57,7 +67,7 @@ export const authClient = createAuthClient({
|
|
|
57
67
|
export const { signIn, signUp, signOut, useSession } = authClient;
|
|
58
68
|
```
|
|
59
69
|
|
|
60
|
-
### With API Key Validation
|
|
70
|
+
### With API Key Validation (KrutAuth class)
|
|
61
71
|
|
|
62
72
|
```typescript
|
|
63
73
|
import { KrutAuth } from "@krutai/auth";
|
|
@@ -76,22 +86,31 @@ await auth.initialize();
|
|
|
76
86
|
|
|
77
87
|
| Import path | What it provides |
|
|
78
88
|
|---|---|
|
|
79
|
-
| `@krutai/auth` | `
|
|
89
|
+
| `@krutai/auth` | `krutAuth`, `KrutAuth`, validators |
|
|
80
90
|
| `@krutai/auth/react` | `createAuthClient`, `useSession`, etc. |
|
|
81
91
|
| `@krutai/auth/next-js` | `toNextJsHandler` |
|
|
82
92
|
|
|
83
93
|
## API Reference
|
|
84
94
|
|
|
85
|
-
### `
|
|
95
|
+
### `krutAuth(options)`
|
|
96
|
+
|
|
97
|
+
Drop-in replacement for `betterAuth`. Accepts the same options.
|
|
86
98
|
|
|
87
|
-
|
|
99
|
+
```typescript
|
|
100
|
+
import { krutAuth } from "@krutai/auth";
|
|
101
|
+
|
|
102
|
+
export const auth = krutAuth({ /* Better Auth options */ });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `KrutAuth` class
|
|
106
|
+
|
|
107
|
+
For API-key-protected authentication.
|
|
88
108
|
|
|
89
109
|
| Option | Type | Required | Description |
|
|
90
110
|
|---|---|---|---|
|
|
91
111
|
| `apiKey` | `string` | ✅ | Your KrutAI API key |
|
|
92
112
|
| `betterAuthOptions` | `object` | — | Better Auth configuration |
|
|
93
113
|
| `validateOnInit` | `boolean` | — | Validate API key on init (default: `true`) |
|
|
94
|
-
| `validationEndpoint` | `string` | — | Custom validation endpoint |
|
|
95
114
|
|
|
96
115
|
#### Methods
|
|
97
116
|
|
|
@@ -115,17 +134,14 @@ try {
|
|
|
115
134
|
}
|
|
116
135
|
```
|
|
117
136
|
|
|
118
|
-
> `ApiKeyValidationError` is re-exported from `krutai` — the single source of truth for API validation across the KrutAI ecosystem.
|
|
119
|
-
|
|
120
137
|
## Architecture
|
|
121
138
|
|
|
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
139
|
```
|
|
125
|
-
@krutai/auth@0.1.
|
|
126
|
-
├──
|
|
127
|
-
├── dependency:
|
|
128
|
-
|
|
140
|
+
@krutai/auth@0.1.7
|
|
141
|
+
├── dependency: krutai ← API key validation
|
|
142
|
+
├── dependency: better-auth ← auth engine
|
|
143
|
+
├── dependency: better-sqlite3 ← default database adapter
|
|
144
|
+
└── dependency: @types/better-sqlite3
|
|
129
145
|
```
|
|
130
146
|
|
|
131
147
|
For Better Auth documentation, visit: https://www.better-auth.com/docs
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
1
2
|
import { BetterAuthOptions, Auth } from 'better-auth';
|
|
2
3
|
export { BetterAuthOptions } from 'better-auth';
|
|
3
4
|
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
@@ -122,35 +123,23 @@ declare class KrutAuth {
|
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
/**
|
|
125
|
-
*
|
|
126
|
+
* krutAuth — drop-in replacement for betterAuth.
|
|
126
127
|
*
|
|
127
|
-
*
|
|
128
|
+
* Use this instead of importing betterAuth directly.
|
|
128
129
|
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* import { KrutAuth } from "@krutai/auth";
|
|
132
|
-
*
|
|
133
|
-
* export const auth = new KrutAuth({
|
|
134
|
-
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
135
|
-
* betterAuthOptions: { ... },
|
|
136
|
-
* });
|
|
137
|
-
* await auth.initialize();
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* @example Client-side (React / Next.js client component)
|
|
130
|
+
* @example
|
|
141
131
|
* ```typescript
|
|
142
|
-
* import {
|
|
132
|
+
* import { krutAuth } from "@krutai/auth";
|
|
133
|
+
* import Database from "better-sqlite3";
|
|
143
134
|
*
|
|
144
|
-
* export const
|
|
145
|
-
*
|
|
135
|
+
* export const auth = krutAuth({
|
|
136
|
+
* database: new Database("./sqlite.db"),
|
|
137
|
+
* emailAndPassword: { enabled: true },
|
|
146
138
|
* });
|
|
147
|
-
*
|
|
148
|
-
* export const { signIn, signUp, signOut, useSession } = authClient;
|
|
149
139
|
* ```
|
|
150
|
-
*
|
|
151
|
-
* @packageDocumentation
|
|
152
140
|
*/
|
|
141
|
+
declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
|
|
153
142
|
|
|
154
|
-
declare const VERSION = "0.1.
|
|
143
|
+
declare const VERSION = "0.1.7";
|
|
155
144
|
|
|
156
|
-
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
|
|
145
|
+
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
1
2
|
import { BetterAuthOptions, Auth } from 'better-auth';
|
|
2
3
|
export { BetterAuthOptions } from 'better-auth';
|
|
3
4
|
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
@@ -122,35 +123,23 @@ declare class KrutAuth {
|
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
/**
|
|
125
|
-
*
|
|
126
|
+
* krutAuth — drop-in replacement for betterAuth.
|
|
126
127
|
*
|
|
127
|
-
*
|
|
128
|
+
* Use this instead of importing betterAuth directly.
|
|
128
129
|
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* import { KrutAuth } from "@krutai/auth";
|
|
132
|
-
*
|
|
133
|
-
* export const auth = new KrutAuth({
|
|
134
|
-
* apiKey: process.env.KRUTAI_API_KEY!,
|
|
135
|
-
* betterAuthOptions: { ... },
|
|
136
|
-
* });
|
|
137
|
-
* await auth.initialize();
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* @example Client-side (React / Next.js client component)
|
|
130
|
+
* @example
|
|
141
131
|
* ```typescript
|
|
142
|
-
* import {
|
|
132
|
+
* import { krutAuth } from "@krutai/auth";
|
|
133
|
+
* import Database from "better-sqlite3";
|
|
143
134
|
*
|
|
144
|
-
* export const
|
|
145
|
-
*
|
|
135
|
+
* export const auth = krutAuth({
|
|
136
|
+
* database: new Database("./sqlite.db"),
|
|
137
|
+
* emailAndPassword: { enabled: true },
|
|
146
138
|
* });
|
|
147
|
-
*
|
|
148
|
-
* export const { signIn, signUp, signOut, useSession } = authClient;
|
|
149
139
|
* ```
|
|
150
|
-
*
|
|
151
|
-
* @packageDocumentation
|
|
152
140
|
*/
|
|
141
|
+
declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
|
|
153
142
|
|
|
154
|
-
declare const VERSION = "0.1.
|
|
143
|
+
declare const VERSION = "0.1.7";
|
|
155
144
|
|
|
156
|
-
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
|
|
145
|
+
export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var betterAuth = require('better-auth');
|
|
4
4
|
var krutai = require('krutai');
|
|
5
5
|
|
|
6
|
-
// src/
|
|
6
|
+
// src/index.ts
|
|
7
7
|
var KrutAuth = class {
|
|
8
8
|
/**
|
|
9
9
|
* Creates a new KrutAuth instance
|
|
@@ -93,7 +93,10 @@ var KrutAuth = class {
|
|
|
93
93
|
return this.getBetterAuth();
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
|
-
|
|
96
|
+
function krutAuth(options) {
|
|
97
|
+
return betterAuth.betterAuth(options);
|
|
98
|
+
}
|
|
99
|
+
var VERSION = "0.1.7";
|
|
97
100
|
|
|
98
101
|
Object.defineProperty(exports, "ApiKeyValidationError", {
|
|
99
102
|
enumerable: true,
|
|
@@ -113,5 +116,6 @@ Object.defineProperty(exports, "validateApiKeyWithService", {
|
|
|
113
116
|
});
|
|
114
117
|
exports.KrutAuth = KrutAuth;
|
|
115
118
|
exports.VERSION = VERSION;
|
|
119
|
+
exports.krutAuth = krutAuth;
|
|
116
120
|
//# sourceMappingURL=index.js.map
|
|
117
121
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/index.ts"],"names":["validateApiKeyFormat","validateApiKeyWithService","betterAuth"],"mappings":";;;;;;AAiCO,IAAM,WAAN,MAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUlB,YAAoB,MAAA,EAAwB;AAAxB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEhB,IAAAA,2BAAA,CAAqB,OAAO,MAAM,CAAA;AAClC,IAAA,IAAA,CAAK,SAAS,MAAA,CAAO,MAAA;AAGrB,IAAA,IAAI,MAAA,CAAO,mBAAmB,KAAA,EAAO;AACjC,MAAA,IAAA,CAAK,oBAAA,EAAqB;AAAA,IAC9B;AAAA,EACJ;AAAA,EAlBQ,MAAA;AAAA,EACA,kBAAA,GAAkC,IAAA;AAAA,EAClC,WAAA,GAAc,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBtB,MAAM,UAAA,GAA4B;AAC9B,IAAA,IAAI,KAAK,WAAA,EAAa;AAClB,MAAA;AAAA,IACJ;AAGA,IAAA,IAAI,IAAA,CAAK,MAAA,CAAO,cAAA,KAAmB,KAAA,EAAO;AACtC,MAAA,MAAMC,gCAAA,CAA0B,KAAK,MAAM,CAAA;AAAA,IAC/C;AAEA,IAAA,IAAA,CAAK,oBAAA,EAAqB;AAC1B,IAAA,IAAA,CAAK,WAAA,GAAc,IAAA;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,oBAAA,GAA6B;AACjC,IAAA,IAAA,CAAK,qBAAqBC,qBAAA,CAAW;AAAA,MACjC,GAAG,KAAK,MAAA,CAAO;AAAA,KAClB,CAAA;AAAA,EACL;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,aAAA,GAAsB;AAClB,IAAA,IAAI,CAAC,KAAK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A,sBAAW,OAAO,CAAA;AAC7B;AAiBO,IAAM,OAAA,GAAU","file":"index.js","sourcesContent":["import { betterAuth } from 'better-auth';\nimport type { Auth } from 'better-auth';\nimport type { KrutAuthConfig } from './types';\n// Import validation from krutai peer dependency\nimport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n} from 'krutai';\n\n/**\n * KrutAuth - Authentication client for KrutAI\n *\n * This class wraps Better Auth and adds API key validation\n * to ensure only authorized users can access authentication features.\n *\n * @example\n * ```typescript\n * import { KrutAuth } from '@krutai/auth';\n *\n * const auth = new KrutAuth({\n * apiKey: 'your-api-key-here',\n * betterAuthOptions: {\n * // Better Auth configuration\n * }\n * });\n *\n * // Initialize the client (validates API key)\n * await auth.initialize();\n *\n * // Use authentication features\n * const betterAuth = auth.getBetterAuth();\n * ```\n */\nexport class KrutAuth {\n private apiKey: string;\n private betterAuthInstance: Auth | null = null;\n private initialized = false;\n\n /**\n * Creates a new KrutAuth instance\n * @param config - Configuration options\n * @throws {ApiKeyValidationError} If API key is invalid\n */\n constructor(private config: KrutAuthConfig) {\n // Validate API key format immediately\n validateApiKeyFormat(config.apiKey);\n this.apiKey = config.apiKey;\n\n // Initialize if validation is not required on init\n if (config.validateOnInit === false) {\n this.initializeBetterAuth();\n }\n }\n\n /**\n * Initialize the authentication client\n * Validates the API key and sets up Better Auth\n * @throws {ApiKeyValidationError} If API key validation fails\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n // Validate API key with service if needed\n if (this.config.validateOnInit !== false) {\n await validateApiKeyWithService(this.apiKey);\n }\n\n this.initializeBetterAuth();\n this.initialized = true;\n }\n\n /**\n * Initialize Better Auth instance\n * @private\n */\n private initializeBetterAuth(): void {\n this.betterAuthInstance = betterAuth({\n ...this.config.betterAuthOptions,\n });\n }\n\n /**\n * Get the Better Auth instance\n * @throws {Error} If not initialized\n */\n getBetterAuth(): Auth {\n if (!this.betterAuthInstance) {\n throw new Error(\n 'KrutAuth not initialized. Call initialize() first or set validateOnInit to false.'\n );\n }\n return this.betterAuthInstance;\n }\n\n /**\n * Check if the client is initialized\n */\n isInitialized(): boolean {\n return this.initialized;\n }\n\n /**\n * Get the API key (useful for making authenticated requests)\n * @returns The API key\n */\n getApiKey(): string {\n return this.apiKey;\n }\n\n /**\n * Sign in a user\n * This is a convenience method that wraps Better Auth\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signIn(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Sign out the current user\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signOut(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Get the current session\n * You can access the full Better Auth API via getBetterAuth()\n */\n async getSession(): Promise<Auth> {\n return this.getBetterAuth();\n }\n}\n","/**\n * @krutai/auth - Authentication package for KrutAI\n *\n * Requires `krutai` as a peer dependency (installed automatically).\n *\n * @example Server-side (Next.js API route / server component)\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * });\n * ```\n *\n * @example Client-side (React / Next.js client component)\n * ```typescript\n * import { createAuthClient } from \"@krutai/auth/react\";\n *\n * export const authClient = createAuthClient({\n * baseURL: process.env.NEXT_PUBLIC_APP_URL ?? \"http://localhost:3000\",\n * });\n *\n * export const { signIn, signUp, signOut, useSession } = authClient;\n * ```\n *\n * @packageDocumentation\n */\n\nimport { betterAuth } from 'better-auth';\nimport type { BetterAuthOptions } from 'better-auth';\n\n/**\n * krutAuth — drop-in replacement for betterAuth.\n *\n * Use this instead of importing betterAuth directly.\n *\n * @example\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * });\n * ```\n */\nexport function krutAuth(options: BetterAuthOptions) {\n return betterAuth(options);\n}\n\n// Export main KrutAuth class (API-key-protected wrapper)\nexport { KrutAuth } from './client';\n\n// Export types\nexport type { KrutAuthConfig, AuthSession, BetterAuthOptions } from './types';\n\n// Re-export validator utilities from krutai peer dependency\nexport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n createApiKeyChecker,\n ApiKeyValidationError,\n} from 'krutai';\n\n// Package metadata\nexport const VERSION = '0.1.7';\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { betterAuth } from 'better-auth';
|
|
|
2
2
|
import { validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
3
3
|
export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
|
|
4
4
|
|
|
5
|
-
// src/
|
|
5
|
+
// src/index.ts
|
|
6
6
|
var KrutAuth = class {
|
|
7
7
|
/**
|
|
8
8
|
* Creates a new KrutAuth instance
|
|
@@ -92,8 +92,11 @@ var KrutAuth = class {
|
|
|
92
92
|
return this.getBetterAuth();
|
|
93
93
|
}
|
|
94
94
|
};
|
|
95
|
-
|
|
95
|
+
function krutAuth(options) {
|
|
96
|
+
return betterAuth(options);
|
|
97
|
+
}
|
|
98
|
+
var VERSION = "0.1.7";
|
|
96
99
|
|
|
97
|
-
export { KrutAuth, VERSION };
|
|
100
|
+
export { KrutAuth, VERSION, krutAuth };
|
|
98
101
|
//# sourceMappingURL=index.mjs.map
|
|
99
102
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/index.ts"],"names":[],"mappings":";;;;;AAiCO,IAAM,WAAN,MAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUlB,YAAoB,MAAA,EAAwB;AAAxB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEhB,IAAA,oBAAA,CAAqB,OAAO,MAAM,CAAA;AAClC,IAAA,IAAA,CAAK,SAAS,MAAA,CAAO,MAA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AAA;AAAA,EAMA,aAAA,GAAsB;AAClB,IAAA,IAAI,CAAC,KAAK,kBAAA,EAAoB;AAC1B,MAAA,MAAM,IAAI,KAAA;AAAA,QACN;AAAA,OACJ;AAAA,IACJ;AACA,IAAA,OAAO,IAAA,CAAK,kBAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA,EAKA,aAAA,GAAyB;AACrB,IAAA,OAAO,IAAA,CAAK,WAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAA,GAAoB;AAChB,IAAA,OAAO,IAAA,CAAK,MAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,MAAA,GAAwB;AAC1B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAA,GAAyB;AAC3B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAA,GAA4B;AAC9B,IAAA,OAAO,KAAK,aAAA,EAAc;AAAA,EAC9B;AACJ;ACtFO,SAAS,SAAS,OAAA,EAA4B;AACjD,EAAA,OAAOA,WAAW,OAAO,CAAA;AAC7B;AAiBO,IAAM,OAAA,GAAU","file":"index.mjs","sourcesContent":["import { betterAuth } from 'better-auth';\nimport type { Auth } from 'better-auth';\nimport type { KrutAuthConfig } from './types';\n// Import validation from krutai peer dependency\nimport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n} from 'krutai';\n\n/**\n * KrutAuth - Authentication client for KrutAI\n *\n * This class wraps Better Auth and adds API key validation\n * to ensure only authorized users can access authentication features.\n *\n * @example\n * ```typescript\n * import { KrutAuth } from '@krutai/auth';\n *\n * const auth = new KrutAuth({\n * apiKey: 'your-api-key-here',\n * betterAuthOptions: {\n * // Better Auth configuration\n * }\n * });\n *\n * // Initialize the client (validates API key)\n * await auth.initialize();\n *\n * // Use authentication features\n * const betterAuth = auth.getBetterAuth();\n * ```\n */\nexport class KrutAuth {\n private apiKey: string;\n private betterAuthInstance: Auth | null = null;\n private initialized = false;\n\n /**\n * Creates a new KrutAuth instance\n * @param config - Configuration options\n * @throws {ApiKeyValidationError} If API key is invalid\n */\n constructor(private config: KrutAuthConfig) {\n // Validate API key format immediately\n validateApiKeyFormat(config.apiKey);\n this.apiKey = config.apiKey;\n\n // Initialize if validation is not required on init\n if (config.validateOnInit === false) {\n this.initializeBetterAuth();\n }\n }\n\n /**\n * Initialize the authentication client\n * Validates the API key and sets up Better Auth\n * @throws {ApiKeyValidationError} If API key validation fails\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n // Validate API key with service if needed\n if (this.config.validateOnInit !== false) {\n await validateApiKeyWithService(this.apiKey);\n }\n\n this.initializeBetterAuth();\n this.initialized = true;\n }\n\n /**\n * Initialize Better Auth instance\n * @private\n */\n private initializeBetterAuth(): void {\n this.betterAuthInstance = betterAuth({\n ...this.config.betterAuthOptions,\n });\n }\n\n /**\n * Get the Better Auth instance\n * @throws {Error} If not initialized\n */\n getBetterAuth(): Auth {\n if (!this.betterAuthInstance) {\n throw new Error(\n 'KrutAuth not initialized. Call initialize() first or set validateOnInit to false.'\n );\n }\n return this.betterAuthInstance;\n }\n\n /**\n * Check if the client is initialized\n */\n isInitialized(): boolean {\n return this.initialized;\n }\n\n /**\n * Get the API key (useful for making authenticated requests)\n * @returns The API key\n */\n getApiKey(): string {\n return this.apiKey;\n }\n\n /**\n * Sign in a user\n * This is a convenience method that wraps Better Auth\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signIn(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Sign out the current user\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signOut(): Promise<Auth> {\n return this.getBetterAuth();\n }\n\n /**\n * Get the current session\n * You can access the full Better Auth API via getBetterAuth()\n */\n async getSession(): Promise<Auth> {\n return this.getBetterAuth();\n }\n}\n","/**\n * @krutai/auth - Authentication package for KrutAI\n *\n * Requires `krutai` as a peer dependency (installed automatically).\n *\n * @example Server-side (Next.js API route / server component)\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * });\n * ```\n *\n * @example Client-side (React / Next.js client component)\n * ```typescript\n * import { createAuthClient } from \"@krutai/auth/react\";\n *\n * export const authClient = createAuthClient({\n * baseURL: process.env.NEXT_PUBLIC_APP_URL ?? \"http://localhost:3000\",\n * });\n *\n * export const { signIn, signUp, signOut, useSession } = authClient;\n * ```\n *\n * @packageDocumentation\n */\n\nimport { betterAuth } from 'better-auth';\nimport type { BetterAuthOptions } from 'better-auth';\n\n/**\n * krutAuth — drop-in replacement for betterAuth.\n *\n * Use this instead of importing betterAuth directly.\n *\n * @example\n * ```typescript\n * import { krutAuth } from \"@krutai/auth\";\n * import Database from \"better-sqlite3\";\n *\n * export const auth = krutAuth({\n * database: new Database(\"./sqlite.db\"),\n * emailAndPassword: { enabled: true },\n * });\n * ```\n */\nexport function krutAuth(options: BetterAuthOptions) {\n return betterAuth(options);\n}\n\n// Export main KrutAuth class (API-key-protected wrapper)\nexport { KrutAuth } from './client';\n\n// Export types\nexport type { KrutAuthConfig, AuthSession, BetterAuthOptions } from './types';\n\n// Re-export validator utilities from krutai peer dependency\nexport {\n validateApiKeyFormat,\n validateApiKeyWithService,\n createApiKeyChecker,\n ApiKeyValidationError,\n} from 'krutai';\n\n// Package metadata\nexport const VERSION = '0.1.7';\n"]}
|
package/package.json
CHANGED