@krutai/auth 0.1.5 → 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 CHANGED
@@ -3,32 +3,33 @@
3
3
  ## Package Overview
4
4
 
5
5
  - **Name**: `@krutai/auth`
6
- - **Version**: `0.1.4`
7
- - **Purpose**: Authentication package for KrutAI — wraps Better Auth with mandatory API key validation
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, `better-auth` bundled, `krutai` external peer dep)
9
+ - **Build**: `tsup` (CJS + ESM, all deps external)
10
10
 
11
11
  ## Dependency Architecture
12
12
 
13
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)
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**: 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.
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 # Barrel export re-exports from krutai for validator
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 # createAuthClient (better-auth/react)
31
- │ └── next-js.ts # toNextJsHandler (better-auth/next-js)
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` | 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` |
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
- ### Classes
48
+ ### `krutAuth(options)` ← PRIMARY API
48
49
 
49
- #### `KrutAuth`
50
- Main authentication client.
50
+ Drop-in replacement for `betterAuth`. Users should always use this.
51
51
 
52
- **Constructor:**
53
52
  ```typescript
54
- new KrutAuth(config: KrutAuthConfig)
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
- ### Other Re-exports
111
+ ## Usage Examples
92
112
 
113
+ ### Example 1: Standard Server Setup (recommended)
93
114
  ```typescript
94
- export { betterAuth } from 'better-auth';
95
- ```
115
+ import { krutAuth } from "@krutai/auth";
116
+ import Database from "better-sqlite3";
96
117
 
97
- ## Usage Examples
118
+ export const auth = krutAuth({
119
+ database: new Database("./sqlite.db"),
120
+ emailAndPassword: { enabled: true },
121
+ });
122
+ ```
98
123
 
99
- ### Example 1: Basic Server Setup
124
+ ### Example 2: Next.js Route Handler
100
125
  ```typescript
101
- import { betterAuth } from '@krutai/auth';
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 auth = betterAuth({
104
- database: { /* config */ },
105
- });
130
+ export const { GET, POST } = toNextJsHandler(auth);
106
131
  ```
107
132
 
108
- ### Example 2: KrutAuth with API Key
133
+ ### Example 3: React Client
109
134
  ```typescript
110
- import { KrutAuth } from '@krutai/auth';
135
+ import { createAuthClient } from "@krutai/auth/react";
111
136
 
112
- const auth = new KrutAuth({
113
- apiKey: process.env.KRUTAI_API_KEY!,
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 3: Skip Async Validation
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
- validateOnInit: false,
129
- betterAuthOptions: { /* config */ },
149
+ betterAuthOptions: { emailAndPassword: { enabled: true } },
130
150
  });
131
- // No need to call initialize()
132
- const betterAuth = auth.getBetterAuth();
151
+ await auth.initialize();
133
152
  ```
134
153
 
135
- ### Example 4: Error Handling
154
+ ### Example 5: Error Handling
136
155
  ```typescript
137
- import { KrutAuth, ApiKeyValidationError } from '@krutai/auth';
156
+ import { KrutAuth, ApiKeyValidationError } from "@krutai/auth";
138
157
 
139
158
  try {
140
- const auth = new KrutAuth({ apiKey: 'bad' });
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('Invalid API key:', e.message);
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` → `noExternal` (bundled into dist)
171
- - `krutai` → external (peer dep, NOT bundled)
172
- - `react`, `react-dom`, `next`, `better-sqlite3` → external
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. **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()`)
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 `krutai`**The core `krutai` package is installed automatically as a peer dependency
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` is automatically installed as a peer dependency. `better-sqlite3` is included as a dependency and `better-auth` is bundled no additional packages required.
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 { betterAuth } from "@krutai/auth";
28
+ import { krutAuth } from "@krutai/auth";
29
+ import Database from "better-sqlite3";
29
30
 
30
- export const auth = betterAuth({
31
- database: {
32
- // your database config
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` | `betterAuth`, `KrutAuth`, validator re-exports from `krutai` |
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
- ### `KrutAuth`
95
+ ### `krutAuth(options)`
96
+
97
+ Drop-in replacement for `betterAuth`. Accepts the same options.
86
98
 
87
- #### Constructor options
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.4
126
- ├── peerDependency: krutai >=0.1.2 (auto-installed)
127
- ├── dependency: better-sqlite3
128
- └── bundled: better-auth
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,5 +1,5 @@
1
1
  import * as better_auth from 'better-auth';
2
- import { BetterAuthOptions, betterAuth } from 'better-auth';
2
+ import { BetterAuthOptions, Auth } from 'better-auth';
3
3
  export { BetterAuthOptions } from 'better-auth';
4
4
  export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
5
5
 
@@ -94,7 +94,7 @@ declare class KrutAuth {
94
94
  * Get the Better Auth instance
95
95
  * @throws {Error} If not initialized
96
96
  */
97
- getBetterAuth(): ReturnType<typeof betterAuth>;
97
+ getBetterAuth(): Auth;
98
98
  /**
99
99
  * Check if the client is initialized
100
100
  */
@@ -109,49 +109,37 @@ declare class KrutAuth {
109
109
  * This is a convenience method that wraps Better Auth
110
110
  * You can access the full Better Auth API via getBetterAuth()
111
111
  */
112
- signIn(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
112
+ signIn(): Promise<Auth>;
113
113
  /**
114
114
  * Sign out the current user
115
115
  * You can access the full Better Auth API via getBetterAuth()
116
116
  */
117
- signOut(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
117
+ signOut(): Promise<Auth>;
118
118
  /**
119
119
  * Get the current session
120
120
  * You can access the full Better Auth API via getBetterAuth()
121
121
  */
122
- getSession(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
122
+ getSession(): Promise<Auth>;
123
123
  }
124
124
 
125
125
  /**
126
- * @krutai/auth - Authentication package for KrutAI
126
+ * krutAuth — drop-in replacement for betterAuth.
127
127
  *
128
- * Requires `krutai` as a peer dependency (installed automatically).
128
+ * Use this instead of importing betterAuth directly.
129
129
  *
130
- * @example Server-side (Next.js API route / server component)
131
- * ```typescript
132
- * import { KrutAuth } from "@krutai/auth";
133
- *
134
- * export const auth = new KrutAuth({
135
- * apiKey: process.env.KRUTAI_API_KEY!,
136
- * betterAuthOptions: { ... },
137
- * });
138
- * await auth.initialize();
139
- * ```
140
- *
141
- * @example Client-side (React / Next.js client component)
130
+ * @example
142
131
  * ```typescript
143
- * import { createAuthClient } from "@krutai/auth/react";
132
+ * import { krutAuth } from "@krutai/auth";
133
+ * import Database from "better-sqlite3";
144
134
  *
145
- * export const authClient = createAuthClient({
146
- * baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
135
+ * export const auth = krutAuth({
136
+ * database: new Database("./sqlite.db"),
137
+ * emailAndPassword: { enabled: true },
147
138
  * });
148
- *
149
- * export const { signIn, signUp, signOut, useSession } = authClient;
150
139
  * ```
151
- *
152
- * @packageDocumentation
153
140
  */
141
+ declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
154
142
 
155
- declare const VERSION = "0.1.5";
143
+ declare const VERSION = "0.1.7";
156
144
 
157
- export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION };
145
+ export { type AuthSession, KrutAuth, type KrutAuthConfig, VERSION, krutAuth };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as better_auth from 'better-auth';
2
- import { BetterAuthOptions, betterAuth } from 'better-auth';
2
+ import { BetterAuthOptions, Auth } from 'better-auth';
3
3
  export { BetterAuthOptions } from 'better-auth';
4
4
  export { ApiKeyValidationError, createApiKeyChecker, validateApiKeyFormat, validateApiKeyWithService } from 'krutai';
5
5
 
@@ -94,7 +94,7 @@ declare class KrutAuth {
94
94
  * Get the Better Auth instance
95
95
  * @throws {Error} If not initialized
96
96
  */
97
- getBetterAuth(): ReturnType<typeof betterAuth>;
97
+ getBetterAuth(): Auth;
98
98
  /**
99
99
  * Check if the client is initialized
100
100
  */
@@ -109,49 +109,37 @@ declare class KrutAuth {
109
109
  * This is a convenience method that wraps Better Auth
110
110
  * You can access the full Better Auth API via getBetterAuth()
111
111
  */
112
- signIn(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
112
+ signIn(): Promise<Auth>;
113
113
  /**
114
114
  * Sign out the current user
115
115
  * You can access the full Better Auth API via getBetterAuth()
116
116
  */
117
- signOut(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
117
+ signOut(): Promise<Auth>;
118
118
  /**
119
119
  * Get the current session
120
120
  * You can access the full Better Auth API via getBetterAuth()
121
121
  */
122
- getSession(): Promise<better_auth.Auth<better_auth.BetterAuthOptions>>;
122
+ getSession(): Promise<Auth>;
123
123
  }
124
124
 
125
125
  /**
126
- * @krutai/auth - Authentication package for KrutAI
126
+ * krutAuth — drop-in replacement for betterAuth.
127
127
  *
128
- * Requires `krutai` as a peer dependency (installed automatically).
128
+ * Use this instead of importing betterAuth directly.
129
129
  *
130
- * @example Server-side (Next.js API route / server component)
131
- * ```typescript
132
- * import { KrutAuth } from "@krutai/auth";
133
- *
134
- * export const auth = new KrutAuth({
135
- * apiKey: process.env.KRUTAI_API_KEY!,
136
- * betterAuthOptions: { ... },
137
- * });
138
- * await auth.initialize();
139
- * ```
140
- *
141
- * @example Client-side (React / Next.js client component)
130
+ * @example
142
131
  * ```typescript
143
- * import { createAuthClient } from "@krutai/auth/react";
132
+ * import { krutAuth } from "@krutai/auth";
133
+ * import Database from "better-sqlite3";
144
134
  *
145
- * export const authClient = createAuthClient({
146
- * baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
135
+ * export const auth = krutAuth({
136
+ * database: new Database("./sqlite.db"),
137
+ * emailAndPassword: { enabled: true },
147
138
  * });
148
- *
149
- * export const { signIn, signUp, signOut, useSession } = authClient;
150
139
  * ```
151
- *
152
- * @packageDocumentation
153
140
  */
141
+ declare function krutAuth(options: BetterAuthOptions): better_auth.Auth<BetterAuthOptions>;
154
142
 
155
- declare const VERSION = "0.1.5";
143
+ declare const VERSION = "0.1.7";
156
144
 
157
- 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/client.ts
6
+ // src/index.ts
7
7
  var KrutAuth = class {
8
8
  /**
9
9
  * Creates a new KrutAuth instance
@@ -76,27 +76,27 @@ var KrutAuth = class {
76
76
  * You can access the full Better Auth API via getBetterAuth()
77
77
  */
78
78
  async signIn() {
79
- const auth = this.getBetterAuth();
80
- return auth;
79
+ return this.getBetterAuth();
81
80
  }
82
81
  /**
83
82
  * Sign out the current user
84
83
  * You can access the full Better Auth API via getBetterAuth()
85
84
  */
86
85
  async signOut() {
87
- const auth = this.getBetterAuth();
88
- return auth;
86
+ return this.getBetterAuth();
89
87
  }
90
88
  /**
91
89
  * Get the current session
92
90
  * You can access the full Better Auth API via getBetterAuth()
93
91
  */
94
92
  async getSession() {
95
- const auth = this.getBetterAuth();
96
- return auth;
93
+ return this.getBetterAuth();
97
94
  }
98
95
  };
99
- var VERSION = "0.1.5";
96
+ function krutAuth(options) {
97
+ return betterAuth.betterAuth(options);
98
+ }
99
+ var VERSION = "0.1.7";
100
100
 
101
101
  Object.defineProperty(exports, "ApiKeyValidationError", {
102
102
  enumerable: true,
@@ -116,5 +116,6 @@ Object.defineProperty(exports, "validateApiKeyWithService", {
116
116
  });
117
117
  exports.KrutAuth = KrutAuth;
118
118
  exports.VERSION = VERSION;
119
+ exports.krutAuth = krutAuth;
119
120
  //# sourceMappingURL=index.js.map
120
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":";;;;;;AAgCO,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,GAA2D,IAAA;AAAA,EAC3D,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,GAA+C;AAC3C,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,GAAS;AACX,IAAA,MAAM,IAAA,GAAO,KAAK,aAAA,EAAc;AAChC,IAAA,OAAO,IAAA;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAA,GAAU;AACZ,IAAA,MAAM,IAAA,GAAO,KAAK,aAAA,EAAc;AAChC,IAAA,OAAO,IAAA;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAA,GAAa;AACf,IAAA,MAAM,IAAA,GAAO,KAAK,aAAA,EAAc;AAChC,IAAA,OAAO,IAAA;AAAA,EACX;AACJ;AC5FO,IAAM,OAAA,GAAU","file":"index.js","sourcesContent":["import { betterAuth } 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: ReturnType<typeof betterAuth> | 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(): ReturnType<typeof betterAuth> {\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() {\n const auth = this.getBetterAuth();\n return auth;\n }\n\n /**\n * Sign out the current user\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signOut() {\n const auth = this.getBetterAuth();\n return auth;\n }\n\n /**\n * Get the current session\n * You can access the full Better Auth API via getBetterAuth()\n */\n async getSession() {\n const auth = this.getBetterAuth();\n return auth;\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 *\n * export const auth = new KrutAuth({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * betterAuthOptions: { ... },\n * });\n * await auth.initialize();\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\n// Export main client (KrutAuth only betterAuth is an internal implementation detail)\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.5';\n"]}
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,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,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/client.ts
5
+ // src/index.ts
6
6
  var KrutAuth = class {
7
7
  /**
8
8
  * Creates a new KrutAuth instance
@@ -75,28 +75,28 @@ var KrutAuth = class {
75
75
  * You can access the full Better Auth API via getBetterAuth()
76
76
  */
77
77
  async signIn() {
78
- const auth = this.getBetterAuth();
79
- return auth;
78
+ return this.getBetterAuth();
80
79
  }
81
80
  /**
82
81
  * Sign out the current user
83
82
  * You can access the full Better Auth API via getBetterAuth()
84
83
  */
85
84
  async signOut() {
86
- const auth = this.getBetterAuth();
87
- return auth;
85
+ return this.getBetterAuth();
88
86
  }
89
87
  /**
90
88
  * Get the current session
91
89
  * You can access the full Better Auth API via getBetterAuth()
92
90
  */
93
91
  async getSession() {
94
- const auth = this.getBetterAuth();
95
- return auth;
92
+ return this.getBetterAuth();
96
93
  }
97
94
  };
98
- var VERSION = "0.1.5";
95
+ function krutAuth(options) {
96
+ return betterAuth(options);
97
+ }
98
+ var VERSION = "0.1.7";
99
99
 
100
- export { KrutAuth, VERSION };
100
+ export { KrutAuth, VERSION, krutAuth };
101
101
  //# sourceMappingURL=index.mjs.map
102
102
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/client.ts","../src/index.ts"],"names":[],"mappings":";;;;;AAgCO,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,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,GAA2D,IAAA;AAAA,EAC3D,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,MAAM,yBAAA,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,qBAAqB,UAAA,CAAW;AAAA,MACjC,GAAG,KAAK,MAAA,CAAO;AAAA,KAClB,CAAA;AAAA,EACL;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,aAAA,GAA+C;AAC3C,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,GAAS;AACX,IAAA,MAAM,IAAA,GAAO,KAAK,aAAA,EAAc;AAChC,IAAA,OAAO,IAAA;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAA,GAAU;AACZ,IAAA,MAAM,IAAA,GAAO,KAAK,aAAA,EAAc;AAChC,IAAA,OAAO,IAAA;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAAA,GAAa;AACf,IAAA,MAAM,IAAA,GAAO,KAAK,aAAA,EAAc;AAChC,IAAA,OAAO,IAAA;AAAA,EACX;AACJ;AC5FO,IAAM,OAAA,GAAU","file":"index.mjs","sourcesContent":["import { betterAuth } 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: ReturnType<typeof betterAuth> | 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(): ReturnType<typeof betterAuth> {\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() {\n const auth = this.getBetterAuth();\n return auth;\n }\n\n /**\n * Sign out the current user\n * You can access the full Better Auth API via getBetterAuth()\n */\n async signOut() {\n const auth = this.getBetterAuth();\n return auth;\n }\n\n /**\n * Get the current session\n * You can access the full Better Auth API via getBetterAuth()\n */\n async getSession() {\n const auth = this.getBetterAuth();\n return auth;\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 *\n * export const auth = new KrutAuth({\n * apiKey: process.env.KRUTAI_API_KEY!,\n * betterAuthOptions: { ... },\n * });\n * await auth.initialize();\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\n// Export main client (KrutAuth only betterAuth is an internal implementation detail)\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.5';\n"]}
1
+ {"version":3,"sources":["../src/client.ts","../src/index.ts"],"names":["betterAuth"],"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,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,MAAM,yBAAA,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,qBAAqB,UAAA,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,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krutai/auth",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Authentication package for KrutAI — installs krutai, better-auth, and better-sqlite3 automatically",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -63,4 +63,4 @@
63
63
  "url": "git+https://github.com/AccountantAIOrg/krut_packages.git",
64
64
  "directory": "packages/auth"
65
65
  }
66
- }
66
+ }