@agentuity/auth 0.0.109 → 0.0.111

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.
Files changed (95) hide show
  1. package/AGENTS.md +82 -28
  2. package/README.md +259 -236
  3. package/dist/agentuity/config.d.ts +2386 -0
  4. package/dist/agentuity/config.d.ts.map +1 -0
  5. package/dist/agentuity/config.js +220 -0
  6. package/dist/agentuity/config.js.map +1 -0
  7. package/dist/agentuity/plugins/api-key.d.ts +152 -0
  8. package/dist/agentuity/plugins/api-key.d.ts.map +1 -0
  9. package/dist/agentuity/plugins/api-key.js +21 -0
  10. package/dist/agentuity/plugins/api-key.js.map +1 -0
  11. package/dist/agentuity/plugins/index.d.ts +23 -0
  12. package/dist/agentuity/plugins/index.d.ts.map +1 -0
  13. package/dist/agentuity/plugins/index.js +10 -0
  14. package/dist/agentuity/plugins/index.js.map +1 -0
  15. package/dist/agentuity/plugins/jwt.d.ts +34 -0
  16. package/dist/agentuity/plugins/jwt.d.ts.map +1 -0
  17. package/dist/agentuity/plugins/jwt.js +11 -0
  18. package/dist/agentuity/plugins/jwt.js.map +1 -0
  19. package/dist/agentuity/plugins/organization.d.ts +355 -0
  20. package/dist/agentuity/plugins/organization.d.ts.map +1 -0
  21. package/dist/agentuity/plugins/organization.js +12 -0
  22. package/dist/agentuity/plugins/organization.js.map +1 -0
  23. package/dist/agentuity/react.d.ts +1375 -0
  24. package/dist/agentuity/react.d.ts.map +1 -0
  25. package/dist/agentuity/react.js +206 -0
  26. package/dist/agentuity/react.js.map +1 -0
  27. package/dist/agentuity/server.d.ts +220 -0
  28. package/dist/agentuity/server.d.ts.map +1 -0
  29. package/dist/agentuity/server.js +505 -0
  30. package/dist/agentuity/server.js.map +1 -0
  31. package/dist/agentuity/types.d.ts +172 -0
  32. package/dist/agentuity/types.d.ts.map +1 -0
  33. package/dist/agentuity/types.js +7 -0
  34. package/dist/agentuity/types.js.map +1 -0
  35. package/dist/index.d.ts +31 -8
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +33 -8
  38. package/dist/index.js.map +1 -1
  39. package/dist/schema.d.ts +2922 -0
  40. package/dist/schema.d.ts.map +1 -0
  41. package/dist/schema.js +223 -0
  42. package/dist/schema.js.map +1 -0
  43. package/dist/types.d.ts +14 -18
  44. package/dist/types.d.ts.map +1 -1
  45. package/dist/types.js +1 -1
  46. package/package.json +15 -39
  47. package/src/agentuity/config.ts +401 -0
  48. package/src/agentuity/plugins/api-key.ts +158 -0
  49. package/src/agentuity/plugins/index.ts +35 -0
  50. package/src/agentuity/plugins/jwt.ts +30 -0
  51. package/src/agentuity/plugins/organization.ts +345 -0
  52. package/src/agentuity/react.tsx +328 -0
  53. package/src/agentuity/server.ts +734 -0
  54. package/src/agentuity/types.ts +201 -0
  55. package/src/index.ts +76 -8
  56. package/src/schema.ts +270 -0
  57. package/src/types.ts +14 -22
  58. package/test/agentuity/config.test.ts +621 -0
  59. package/test/agentuity/server.test.ts +537 -0
  60. package/test/schema.test.ts +147 -0
  61. package/tsconfig.json +3 -2
  62. package/tsconfig.tsbuildinfo +1 -1
  63. package/dist/auth0/client.d.ts +0 -44
  64. package/dist/auth0/client.d.ts.map +0 -1
  65. package/dist/auth0/client.js +0 -79
  66. package/dist/auth0/client.js.map +0 -1
  67. package/dist/auth0/index.d.ts +0 -35
  68. package/dist/auth0/index.d.ts.map +0 -1
  69. package/dist/auth0/index.js +0 -38
  70. package/dist/auth0/index.js.map +0 -1
  71. package/dist/auth0/server.d.ts +0 -91
  72. package/dist/auth0/server.d.ts.map +0 -1
  73. package/dist/auth0/server.js +0 -237
  74. package/dist/auth0/server.js.map +0 -1
  75. package/dist/clerk/client.d.ts +0 -42
  76. package/dist/clerk/client.d.ts.map +0 -1
  77. package/dist/clerk/client.js +0 -65
  78. package/dist/clerk/client.js.map +0 -1
  79. package/dist/clerk/index.d.ts +0 -37
  80. package/dist/clerk/index.d.ts.map +0 -1
  81. package/dist/clerk/index.js +0 -35
  82. package/dist/clerk/index.js.map +0 -1
  83. package/dist/clerk/server.d.ts +0 -55
  84. package/dist/clerk/server.d.ts.map +0 -1
  85. package/dist/clerk/server.js +0 -111
  86. package/dist/clerk/server.js.map +0 -1
  87. package/docs/adding-providers.md +0 -261
  88. package/src/auth0/client.tsx +0 -109
  89. package/src/auth0/index.ts +0 -40
  90. package/src/auth0/server.ts +0 -378
  91. package/src/clerk/client.tsx +0 -86
  92. package/src/clerk/index.ts +0 -37
  93. package/src/clerk/server.ts +0 -168
  94. package/test/clerk-client.test.tsx +0 -21
  95. package/test/clerk-server.test.ts +0 -51
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/agentuity/react.tsx"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAkE,MAAM,OAAO,CAAC;AACvF,OAAO,EAAE,gBAAgB,IAAI,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAG/E,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAEjE,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAMrD;;;;GAIG;AACH,MAAM,WAAW,iBAAiB,CACjC,QAAQ,SAAS,sBAAsB,EAAE,GAAG,sBAAsB,EAAE;IAEpE;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B;;;;;OAKG;IACH,OAAO,CAAC,EAAE,QAAQ,CAAC;CACnB;AAED;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB;;;;;;;;;;;;;;;;;;;;gCAJG,CAAC;;;;;gCAIb,CAAC;;;;;gCAKG,CAAC;;;;;gCAGb,CAAA;;;;;gCAEZ,CAAC;;;;;;;;;;;;;;;;;;;;;;6BAgPS,CAAC;;;;;;;;;;;;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CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,EAC5B,QAAQ,EACR,UAAU,EACV,eAAuB,EACvB,aAAwB,GACxB,EAAE,iBAAiB,2CA4EnB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,OAAO,IAAI,gBAAgB,CAM1C"}
@@ -0,0 +1,206 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /**
3
+ * Auth React integration for @agentuity/auth.
4
+ *
5
+ * All React-specific code for auth.
6
+ * Import from '@agentuity/auth/react' for React components and hooks.
7
+ *
8
+ * @module agentuity/react
9
+ */
10
+ import { useEffect, createContext, useContext, useState, useMemo } from 'react';
11
+ import { createAuthClient as createBetterAuthClient } from 'better-auth/react';
12
+ import { organizationClient, apiKeyClient } from 'better-auth/client/plugins';
13
+ import { useAuth as useAgentuityReactAuth } from '@agentuity/react';
14
+ /**
15
+ * Get the default client plugins for auth.
16
+ *
17
+ * These mirror the server-side plugins:
18
+ * - organizationClient: Multi-tenancy support
19
+ * - apiKeyClient: Programmatic API key management
20
+ *
21
+ * Note: jwt() and bearer() are server-only plugins.
22
+ */
23
+ export function getDefaultClientPlugins() {
24
+ return [organizationClient(), apiKeyClient()];
25
+ }
26
+ /**
27
+ * Create a pre-configured Auth client.
28
+ *
29
+ * This factory provides sensible defaults for Agentuity projects:
30
+ * - Uses `/api/auth` as the default base path
31
+ * - Automatically uses `window.location.origin` as base URL in browsers
32
+ * - Includes organization and API key plugins by default
33
+ *
34
+ * @example Basic usage (zero config)
35
+ * ```typescript
36
+ * import { createAuthClient } from '@agentuity/auth/react';
37
+ *
38
+ * export const authClient = createAuthClient();
39
+ * export const { signIn, signUp, signOut, useSession, getSession } = authClient;
40
+ * ```
41
+ *
42
+ * @example With custom base path
43
+ * ```typescript
44
+ * export const authClient = createAuthClient({
45
+ * basePath: '/auth', // If mounted at /auth instead of /api/auth
46
+ * });
47
+ * ```
48
+ *
49
+ * @example With additional plugins
50
+ * ```typescript
51
+ * import { twoFactorClient } from 'better-auth/client/plugins';
52
+ *
53
+ * export const authClient = createAuthClient({
54
+ * plugins: [twoFactorClient()],
55
+ * });
56
+ * ```
57
+ *
58
+ * @example With custom plugins only (no defaults)
59
+ * ```typescript
60
+ * import { organizationClient } from 'better-auth/client/plugins';
61
+ *
62
+ * export const authClient = createAuthClient({
63
+ * skipDefaultPlugins: true,
64
+ * plugins: [organizationClient()],
65
+ * });
66
+ * ```
67
+ */
68
+ export function createAuthClient(options) {
69
+ const baseURL = options?.baseURL ?? (typeof window !== 'undefined' ? window.location.origin : '');
70
+ const basePath = options?.basePath ?? '/api/auth';
71
+ const defaultPlugins = options?.skipDefaultPlugins ? [] : getDefaultClientPlugins();
72
+ const userPlugins = options?.plugins ?? [];
73
+ // Merge default plugins with user plugins
74
+ // We pass through the full options to preserve type inference
75
+ // The return type preserves plugin type inference via the generic parameter
76
+ return createBetterAuthClient({
77
+ ...options,
78
+ baseURL,
79
+ basePath,
80
+ plugins: [...defaultPlugins, ...userPlugins],
81
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
82
+ });
83
+ }
84
+ const AuthContext = createContext(null);
85
+ /**
86
+ * Auth provider component.
87
+ *
88
+ * This component integrates Auth with Agentuity's React context,
89
+ * automatically injecting auth tokens into API calls via useAgent and useWebsocket.
90
+ *
91
+ * Must be a child of AgentuityProvider.
92
+ *
93
+ * @example
94
+ * ```tsx
95
+ * import { AgentuityProvider } from '@agentuity/react';
96
+ * import { createAuthClient, AuthProvider } from '@agentuity/auth/react';
97
+ *
98
+ * const authClient = createAuthClient();
99
+ *
100
+ * <AgentuityProvider>
101
+ * <AuthProvider authClient={authClient}>
102
+ * <App />
103
+ * </AuthProvider>
104
+ * </AgentuityProvider>
105
+ * ```
106
+ */
107
+ export function AuthProvider({ children, authClient, refreshInterval = 60000, tokenEndpoint = '/token', }) {
108
+ const { setAuthHeader, setAuthLoading } = useAgentuityReactAuth();
109
+ const [user, setUser] = useState(null);
110
+ const [session, setSession] = useState(null);
111
+ const [isPending, setIsPending] = useState(true);
112
+ const [error, setError] = useState(null);
113
+ useEffect(() => {
114
+ if (!setAuthHeader || !setAuthLoading)
115
+ return;
116
+ const fetchAuthState = async () => {
117
+ try {
118
+ setAuthLoading(true);
119
+ setIsPending(true);
120
+ setError(null);
121
+ // Use the auth client's getSession method
122
+ const result = await authClient.getSession();
123
+ if (result.data?.user) {
124
+ setUser(result.data.user);
125
+ setSession(result.data.session ?? null);
126
+ // Get the JWT token for API calls (unless disabled)
127
+ if (tokenEndpoint !== false) {
128
+ try {
129
+ const tokenResult = await authClient.$fetch(tokenEndpoint, { method: 'GET' });
130
+ const tokenData = tokenResult.data;
131
+ if (tokenData?.token) {
132
+ setAuthHeader(`Bearer ${tokenData.token}`);
133
+ }
134
+ else {
135
+ setAuthHeader(null);
136
+ }
137
+ }
138
+ catch {
139
+ // Token endpoint might not exist, that's okay
140
+ setAuthHeader(null);
141
+ }
142
+ }
143
+ else {
144
+ setAuthHeader(null);
145
+ }
146
+ }
147
+ else {
148
+ setUser(null);
149
+ setSession(null);
150
+ setAuthHeader(null);
151
+ }
152
+ }
153
+ catch (err) {
154
+ console.error('[AuthProvider] Failed to get auth state:', err);
155
+ setError(err instanceof Error ? err : new Error('Failed to get auth state'));
156
+ setUser(null);
157
+ setSession(null);
158
+ setAuthHeader(null);
159
+ }
160
+ finally {
161
+ setAuthLoading(false);
162
+ setIsPending(false);
163
+ }
164
+ };
165
+ fetchAuthState();
166
+ const interval = setInterval(fetchAuthState, refreshInterval);
167
+ return () => clearInterval(interval);
168
+ }, [authClient, refreshInterval, tokenEndpoint, setAuthHeader, setAuthLoading]);
169
+ const contextValue = useMemo(() => ({
170
+ authClient,
171
+ user,
172
+ session,
173
+ isPending,
174
+ error,
175
+ isAuthenticated: !isPending && user !== null,
176
+ }), [authClient, user, session, isPending, error]);
177
+ return _jsx(AuthContext.Provider, { value: contextValue, children: children });
178
+ }
179
+ /**
180
+ * Hook to access Auth state.
181
+ *
182
+ * This hook provides access to the current user and session.
183
+ * Must be used within an AuthProvider.
184
+ *
185
+ * @example
186
+ * ```tsx
187
+ * import { useAuth } from '@agentuity/auth/react';
188
+ *
189
+ * function Profile() {
190
+ * const { user, session, isPending, isAuthenticated } = useAuth();
191
+ *
192
+ * if (isPending) return <div>Loading...</div>;
193
+ * if (!isAuthenticated) return <div>Not signed in</div>;
194
+ *
195
+ * return <div>Welcome, {user.name}!</div>;
196
+ * }
197
+ * ```
198
+ */
199
+ export function useAuth() {
200
+ const context = useContext(AuthContext);
201
+ if (!context) {
202
+ throw new Error('useAuth must be used within an AuthProvider');
203
+ }
204
+ return context;
205
+ }
206
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sourceRoot":"","sources":["../../src/agentuity/react.tsx"],"names":[],"mappings":";AAAA;;;;;;;GAOG;AAEH,OAAc,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AACvF,OAAO,EAAE,gBAAgB,IAAI,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC9E,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AA4CpE;;;;;;;;GAQG;AACH,MAAM,UAAU,uBAAuB;IACtC,OAAO,CAAC,kBAAkB,EAAE,EAAE,YAAY,EAAE,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,UAAU,gBAAgB,CAC/B,OAAqC;IAErC,MAAM,OAAO,GACZ,OAAO,EAAE,OAAO,IAAI,CAAC,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC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
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Auth Hono middleware and handlers for @agentuity/auth.
3
+ *
4
+ * Provides session and API key authentication middleware for Hono applications.
5
+ *
6
+ * @module agentuity/server
7
+ */
8
+ import type { Context, MiddlewareHandler } from 'hono';
9
+ import type { AuthBase } from './config';
10
+ import type { AuthUser, AuthSession, AuthOrgContext, AuthInterface } from './types';
11
+ /**
12
+ * Configuration for OpenTelemetry span attributes.
13
+ * All attributes are included by default. Set to `false` to opt-out of specific PII.
14
+ */
15
+ export interface OtelSpansConfig {
16
+ /**
17
+ * Include user email in spans (`auth.user.email`).
18
+ * @default true
19
+ */
20
+ email?: boolean;
21
+ /**
22
+ * Include organization name in spans (`auth.org.name`).
23
+ * @default true
24
+ */
25
+ orgName?: boolean;
26
+ }
27
+ export interface AuthMiddlewareOptions {
28
+ /**
29
+ * If true, don't return 401 on missing auth - just continue without auth context.
30
+ * Useful for routes that work for both authenticated and anonymous users.
31
+ */
32
+ optional?: boolean;
33
+ /**
34
+ * Configure which attributes are included in OpenTelemetry spans.
35
+ * All PII attributes are included by default. Use this to opt-out of specific fields.
36
+ *
37
+ * @example Disable email in spans
38
+ * ```typescript
39
+ * createSessionMiddleware(auth, { otelSpans: { email: false } })
40
+ * ```
41
+ */
42
+ otelSpans?: OtelSpansConfig;
43
+ /**
44
+ * Require that the authenticated user has one of the given org roles.
45
+ * If the user is authenticated but lacks the required role, a 403 is returned.
46
+ * Only applies when authentication succeeds (ignored for optional + anonymous).
47
+ *
48
+ * @example Require admin or owner role
49
+ * ```typescript
50
+ * createSessionMiddleware(auth, { hasOrgRole: ['admin', 'owner'] })
51
+ * ```
52
+ */
53
+ hasOrgRole?: string | string[];
54
+ }
55
+ export interface ApiKeyMiddlewareOptions {
56
+ /**
57
+ * If true, don't return 401 on missing/invalid API key - just continue without auth context.
58
+ */
59
+ optional?: boolean;
60
+ /**
61
+ * Configure which attributes are included in OpenTelemetry spans.
62
+ * All PII attributes are included by default. Use this to opt-out of specific fields.
63
+ *
64
+ * @example Disable email in spans
65
+ * ```typescript
66
+ * createApiKeyMiddleware(auth, { otelSpans: { email: false } })
67
+ * ```
68
+ */
69
+ otelSpans?: OtelSpansConfig;
70
+ /**
71
+ * Require that the API key has specific permissions.
72
+ * If the API key lacks any required permission, a 403 is returned.
73
+ *
74
+ * @example Require project write permission
75
+ * ```typescript
76
+ * createApiKeyMiddleware(auth, { hasPermission: { project: 'write' } })
77
+ * ```
78
+ *
79
+ * @example Require multiple permissions
80
+ * ```typescript
81
+ * createApiKeyMiddleware(auth, {
82
+ * hasPermission: { project: ['read', 'write'], admin: '*' }
83
+ * })
84
+ * ```
85
+ */
86
+ hasPermission?: Record<string, string | string[]>;
87
+ }
88
+ /**
89
+ * Hono context variables set by the middleware.
90
+ */
91
+ export type AuthEnv = {
92
+ Variables: {
93
+ auth: AuthInterface<AuthUser>;
94
+ user: AuthUser | null;
95
+ authSession: AuthSession | null;
96
+ org: AuthOrgContext | null;
97
+ };
98
+ };
99
+ /**
100
+ * Create Hono middleware that validates sessions.
101
+ *
102
+ * Sets context variables (`user`, `session`, `org`, `auth`) for authenticated requests.
103
+ *
104
+ * OpenTelemetry spans are automatically enriched with auth attributes:
105
+ * - `auth.user.id` - User ID (always included)
106
+ * - `auth.user.email` - User email (included by default, opt-out via `otelSpans.email: false`)
107
+ * - `auth.method` - 'session' or 'bearer' (always included)
108
+ * - `auth.provider` - 'Auth' (always included)
109
+ * - `auth.org.id` - Active organization ID (always included if set)
110
+ * - `auth.org.name` - Organization name (included by default, opt-out via `otelSpans.orgName: false`)
111
+ *
112
+ * @example Basic usage
113
+ * ```typescript
114
+ * import { createSessionMiddleware } from '@agentuity/auth';
115
+ * import { auth } from './auth';
116
+ *
117
+ * const app = new Hono();
118
+ * app.use('/api/*', createSessionMiddleware(auth));
119
+ *
120
+ * app.get('/api/me', (c) => {
121
+ * const user = c.var.user;
122
+ * if (!user) return c.json({ error: 'Unauthorized' }, 401);
123
+ * return c.json({ id: user.id });
124
+ * });
125
+ * ```
126
+ *
127
+ * @example Using auth wrapper with org role check
128
+ * ```typescript
129
+ * app.get('/api/admin', createSessionMiddleware(auth, { hasOrgRole: ['admin', 'owner'] }), async (c) => {
130
+ * const user = await c.var.auth.getUser();
131
+ * return c.json({ id: user.id, message: 'Welcome admin!' });
132
+ * });
133
+ * ```
134
+ */
135
+ export declare function createSessionMiddleware(auth: AuthBase, options?: AuthMiddlewareOptions): MiddlewareHandler<AuthEnv>;
136
+ /**
137
+ * Create Hono middleware that validates API keys.
138
+ *
139
+ * This middleware ONLY accepts API key authentication via:
140
+ * - `x-agentuity-auth-api-key` header (preferred)
141
+ * - `Authorization: ApiKey <key>` header
142
+ *
143
+ * It does NOT use sessions. For routes that accept both session and API key,
144
+ * compose with createSessionMiddleware using `{ optional: true }`.
145
+ *
146
+ * @example API key only route with permission check
147
+ * ```typescript
148
+ * import { createApiKeyMiddleware } from '@agentuity/auth';
149
+ *
150
+ * app.post('/webhooks/*', createApiKeyMiddleware(auth, {
151
+ * hasPermission: { webhook: 'write' }
152
+ * }));
153
+ *
154
+ * app.post('/webhooks/github', async (c) => {
155
+ * // Permission already verified by middleware
156
+ * return c.json({ success: true });
157
+ * });
158
+ * ```
159
+ *
160
+ * @example Either session OR API key (compose with optional)
161
+ * ```typescript
162
+ * app.use('/api/*', createSessionMiddleware(auth, { optional: true }));
163
+ * app.use('/api/*', createApiKeyMiddleware(auth, { optional: true }));
164
+ *
165
+ * app.get('/api/data', async (c) => {
166
+ * // Works with session OR API key
167
+ * if (!c.var.user) return c.json({ error: 'Unauthorized' }, 401);
168
+ * return c.json({ data: '...' });
169
+ * });
170
+ * ```
171
+ */
172
+ export declare function createApiKeyMiddleware(auth: AuthBase, options?: ApiKeyMiddlewareOptions): MiddlewareHandler<AuthEnv>;
173
+ /**
174
+ * Configuration options for mounting auth routes.
175
+ */
176
+ export interface MountAuthRoutesOptions {
177
+ /**
178
+ * Headers to forward from auth responses to the client.
179
+ * Only headers in this list will be forwarded (case-insensitive).
180
+ * `set-cookie` is always forwarded with append behavior regardless of this setting.
181
+ *
182
+ * @default ['set-cookie', 'content-type', 'location', 'cache-control', 'pragma', 'expires', 'vary', 'etag', 'last-modified']
183
+ */
184
+ allowList?: string[];
185
+ }
186
+ /**
187
+ * Mount auth routes with proper cookie handling and header filtering.
188
+ *
189
+ * This wrapper handles cookie merging between auth responses and other middleware.
190
+ * It ensures both session cookies AND other cookies (like thread cookies)
191
+ * are preserved while preventing unintended headers from leaking through.
192
+ *
193
+ * @example Basic usage
194
+ * ```typescript
195
+ * import { mountAuthRoutes } from '@agentuity/auth';
196
+ * import { auth } from './auth';
197
+ *
198
+ * const api = createRouter();
199
+ *
200
+ * // Mount all auth routes (sign-in, sign-up, sign-out, session, etc.)
201
+ * api.on(['GET', 'POST'], '/api/auth/*', mountAuthRoutes(auth));
202
+ * ```
203
+ *
204
+ * @example With custom header allowlist
205
+ * ```typescript
206
+ * api.on(['GET', 'POST'], '/api/auth/*', mountAuthRoutes(auth, {
207
+ * allowList: ['set-cookie', 'content-type', 'location', 'x-custom-header']
208
+ * }));
209
+ * ```
210
+ */
211
+ export declare function mountAuthRoutes(auth: AuthBase, options?: MountAuthRoutesOptions): (c: Context) => Promise<Response>;
212
+ declare module 'hono' {
213
+ interface ContextVariableMap {
214
+ auth: AuthInterface<AuthUser>;
215
+ user: AuthUser | null;
216
+ authSession: AuthSession | null;
217
+ org: AuthOrgContext | null;
218
+ }
219
+ }
220
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/agentuity/server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAGvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,KAAK,EACX,QAAQ,EACR,WAAW,EACX,cAAc,EAGd,aAAa,EACb,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