@logickernel/bridge 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +276 -0
- package/dist/index.cjs +193 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +195 -0
- package/dist/index.d.ts +195 -0
- package/dist/index.js +175 -0
- package/dist/index.js.map +1 -0
- package/dist/next/index.cjs +424 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +345 -0
- package/dist/next/index.d.ts +345 -0
- package/dist/next/index.js +410 -0
- package/dist/next/index.js.map +1 -0
- package/dist/types-YvOY9KNP.d.cts +88 -0
- package/dist/types-YvOY9KNP.d.ts +88 -0
- package/package.json +75 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { b as Session, S as SessionCookie, M as MicroFrontendConfig } from '../types-YvOY9KNP.cjs';
|
|
2
|
+
export { d as SessionUser } from '../types-YvOY9KNP.cjs';
|
|
3
|
+
import { NextRequest } from 'next/server';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Kernel Bridge Next.js Session
|
|
7
|
+
* Server-side session utilities for Next.js
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get the session cookie from Next.js cookies
|
|
12
|
+
*/
|
|
13
|
+
declare function getSessionCookie(): Promise<SessionCookie | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Get the session token value from cookies
|
|
16
|
+
*/
|
|
17
|
+
declare function getSessionToken(): Promise<string | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Check if secure cookie is being used
|
|
20
|
+
*/
|
|
21
|
+
declare function isSecureCookie(): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Get the current session from cookies
|
|
24
|
+
*
|
|
25
|
+
* This reads the NextAuth session cookie and decodes the JWT.
|
|
26
|
+
* Use this in Server Components, Server Actions, or API routes.
|
|
27
|
+
*
|
|
28
|
+
* @returns Session object or null if not authenticated
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { getSession } from "@logickernel/bridge/next"
|
|
33
|
+
*
|
|
34
|
+
* export default async function Page() {
|
|
35
|
+
* const session = await getSession()
|
|
36
|
+
* if (!session) {
|
|
37
|
+
* redirect("/auth/signin")
|
|
38
|
+
* }
|
|
39
|
+
* return <div>Hello {session.user.email}</div>
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function getSession(): Promise<Session | null>;
|
|
44
|
+
/**
|
|
45
|
+
* Get the base URL (facade/load balancer) for user-facing links and redirects
|
|
46
|
+
*
|
|
47
|
+
* This is the URL that users access through the facade/load balancer.
|
|
48
|
+
* Use this for:
|
|
49
|
+
* - Sign-in redirect URLs
|
|
50
|
+
* - User-facing links to the kernel
|
|
51
|
+
* - Any URLs that users will see or click
|
|
52
|
+
*/
|
|
53
|
+
declare function getBaseUrl(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Get user roles in an organization
|
|
56
|
+
*
|
|
57
|
+
* Fetches roles from the kernel API using the current session.
|
|
58
|
+
* The user ID is determined from the session token.
|
|
59
|
+
*
|
|
60
|
+
* @param organizationId - Organization ID to fetch roles in
|
|
61
|
+
* @returns Array of role names (empty if error or no roles)
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { getUserRoles } from "@logickernel/bridge/next"
|
|
66
|
+
*
|
|
67
|
+
* const roles = await getUserRoles("org-123")
|
|
68
|
+
* const isOwner = roles.includes("organization.owner")
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function getUserRoles(organizationId: string): Promise<string[]>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Kernel Bridge Next.js Types
|
|
75
|
+
* Next.js-specific type definitions
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Options for the proxy factory
|
|
80
|
+
*/
|
|
81
|
+
interface ProxyOptions extends MicroFrontendConfig {
|
|
82
|
+
/**
|
|
83
|
+
* Dynamic route patterns that contain organization IDs
|
|
84
|
+
* e.g., "/[organization_id]/admin" matches "/abc123/admin"
|
|
85
|
+
*/
|
|
86
|
+
dynamicRoutes?: DynamicRoutePattern[];
|
|
87
|
+
/**
|
|
88
|
+
* Custom error page path (defaults to showing error in redirect)
|
|
89
|
+
*/
|
|
90
|
+
errorPath?: string;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Dynamic route pattern configuration
|
|
94
|
+
*/
|
|
95
|
+
interface DynamicRoutePattern {
|
|
96
|
+
/**
|
|
97
|
+
* Pattern to match (e.g., "/[organization_id]/admin")
|
|
98
|
+
* Use [param] syntax for dynamic segments
|
|
99
|
+
*/
|
|
100
|
+
pattern: string;
|
|
101
|
+
/**
|
|
102
|
+
* Required roles for this route (empty = any authenticated user)
|
|
103
|
+
*/
|
|
104
|
+
requiredRoles: string[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Result from parsing a dynamic route
|
|
108
|
+
*/
|
|
109
|
+
interface DynamicRouteMatch {
|
|
110
|
+
matched: boolean;
|
|
111
|
+
params: Record<string, string>;
|
|
112
|
+
requiredRoles: string[];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* API route handler with auth context
|
|
116
|
+
*/
|
|
117
|
+
type AuthenticatedHandler = (request: NextRequest, context: AuthContext) => Promise<Response> | Response;
|
|
118
|
+
/**
|
|
119
|
+
* Context provided to authenticated handlers
|
|
120
|
+
*/
|
|
121
|
+
interface AuthContext {
|
|
122
|
+
session: Session;
|
|
123
|
+
params?: Record<string, string>;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Options for withAuth wrapper
|
|
127
|
+
*/
|
|
128
|
+
interface WithAuthOptions {
|
|
129
|
+
/**
|
|
130
|
+
* Required roles (empty = any authenticated user)
|
|
131
|
+
*/
|
|
132
|
+
requiredRoles?: string[];
|
|
133
|
+
/**
|
|
134
|
+
* Organization ID parameter name in dynamic routes
|
|
135
|
+
*/
|
|
136
|
+
organizationId?: string;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Page auth result
|
|
140
|
+
*/
|
|
141
|
+
type PageAuthResult = {
|
|
142
|
+
authenticated: true;
|
|
143
|
+
session: Session;
|
|
144
|
+
roles: string[];
|
|
145
|
+
} | {
|
|
146
|
+
authenticated: false;
|
|
147
|
+
redirectUrl: string;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Options for page auth check
|
|
151
|
+
*/
|
|
152
|
+
interface PageAuthOptions {
|
|
153
|
+
/**
|
|
154
|
+
* Required roles (empty = any authenticated user)
|
|
155
|
+
*/
|
|
156
|
+
requiredRoles?: string[];
|
|
157
|
+
/**
|
|
158
|
+
* Organization ID for role checking
|
|
159
|
+
*/
|
|
160
|
+
organizationId?: string;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Kernel Bridge Next.js Page
|
|
165
|
+
* Authentication utilities for pages/server components
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Check authentication and optionally roles for a page
|
|
170
|
+
*
|
|
171
|
+
* This is a helper for pages that need both auth and role checking.
|
|
172
|
+
* For simple auth-only checks, use getSession() directly.
|
|
173
|
+
*
|
|
174
|
+
* @param options - Authentication options
|
|
175
|
+
* @returns Auth result with session/roles or redirect URL
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* import { checkPageAuth } from "@logickernel/bridge/next"
|
|
180
|
+
* import { redirect } from "next/navigation"
|
|
181
|
+
*
|
|
182
|
+
* export default async function AdminPage({ params }) {
|
|
183
|
+
* const { organization_id } = await params
|
|
184
|
+
*
|
|
185
|
+
* const auth = await checkPageAuth({
|
|
186
|
+
* requiredRoles: ["organization.owner"],
|
|
187
|
+
* organizationId: organization_id,
|
|
188
|
+
* })
|
|
189
|
+
*
|
|
190
|
+
* if (!auth.authenticated) {
|
|
191
|
+
* redirect(auth.redirectUrl)
|
|
192
|
+
* }
|
|
193
|
+
*
|
|
194
|
+
* return <div>Welcome {auth.session.user.email}</div>
|
|
195
|
+
* }
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
declare function checkPageAuth(options?: PageAuthOptions): Promise<PageAuthResult>;
|
|
199
|
+
/**
|
|
200
|
+
* Require authentication for a page
|
|
201
|
+
*
|
|
202
|
+
* This is a simpler helper that throws if not authenticated.
|
|
203
|
+
* Use with redirect() in the catch block.
|
|
204
|
+
*
|
|
205
|
+
* @returns Session if authenticated
|
|
206
|
+
* @throws RedirectInfo if not authenticated
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* import { requireAuth } from "@logickernel/bridge/next"
|
|
211
|
+
* import { redirect } from "next/navigation"
|
|
212
|
+
*
|
|
213
|
+
* export default async function DashboardPage() {
|
|
214
|
+
* const session = await requireAuth()
|
|
215
|
+
* // If we get here, user is authenticated
|
|
216
|
+
* return <div>Hello {session.user.email}</div>
|
|
217
|
+
* }
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
declare function requireAuth(): Promise<Session>;
|
|
221
|
+
/**
|
|
222
|
+
* Check if user has any of the required roles
|
|
223
|
+
*
|
|
224
|
+
* The user ID is determined from the session token in cookies.
|
|
225
|
+
*
|
|
226
|
+
* @param organizationId - Organization to check roles in
|
|
227
|
+
* @param requiredRoles - Roles to check for
|
|
228
|
+
* @returns True if user has at least one required role
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* const hasAccess = await hasRequiredRole(
|
|
233
|
+
* "org-123",
|
|
234
|
+
* ["organization.owner", "organization.editor"]
|
|
235
|
+
* )
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
declare function hasRequiredRole(organizationId: string, requiredRoles: string[]): Promise<boolean>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Kernel Bridge Next.js API
|
|
242
|
+
* Authentication utilities for API routes
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Wrap an API route handler with authentication
|
|
247
|
+
*
|
|
248
|
+
* This handles JWT validation and optionally role checking.
|
|
249
|
+
* The handler receives the session and can focus on business logic.
|
|
250
|
+
*
|
|
251
|
+
* @param handler - The authenticated handler function
|
|
252
|
+
* @param options - Authentication options
|
|
253
|
+
* @returns A Next.js route handler
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* // Any authenticated user
|
|
258
|
+
* export const GET = withAuth(async (request, { session }) => {
|
|
259
|
+
* return NextResponse.json({ user: session.user })
|
|
260
|
+
* })
|
|
261
|
+
*
|
|
262
|
+
* // With role requirement
|
|
263
|
+
* export const DELETE = withAuth(
|
|
264
|
+
* async (request, { session }) => {
|
|
265
|
+
* return NextResponse.json({ deleted: true })
|
|
266
|
+
* },
|
|
267
|
+
* { requiredRoles: ["organization.owner"] }
|
|
268
|
+
* )
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
declare function withAuth(handler: AuthenticatedHandler, options?: WithAuthOptions): (request: NextRequest, context?: {
|
|
272
|
+
params?: Promise<Record<string, string>>;
|
|
273
|
+
}) => Promise<Response>;
|
|
274
|
+
/**
|
|
275
|
+
* Get session from a request (for manual handling)
|
|
276
|
+
*
|
|
277
|
+
* Use this when you need more control than withAuth provides.
|
|
278
|
+
*
|
|
279
|
+
* @param request - The Next.js request
|
|
280
|
+
* @returns Session or null
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* export async function GET(request: NextRequest) {
|
|
285
|
+
* const session = await getSessionFromRequest(request)
|
|
286
|
+
* if (!session) {
|
|
287
|
+
* return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
288
|
+
* }
|
|
289
|
+
* // Custom logic...
|
|
290
|
+
* }
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
declare function getSessionFromRequest(request: NextRequest): Promise<Session | null>;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Kernel Bridge Next.js Proxy
|
|
297
|
+
* Middleware/proxy utilities for Next.js
|
|
298
|
+
*/
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Create a proxy/middleware handler function for Next.js
|
|
302
|
+
*
|
|
303
|
+
* This creates a fully configured middleware handler that:
|
|
304
|
+
* - Validates authentication for protected routes
|
|
305
|
+
* - Handles dynamic routes with organization IDs
|
|
306
|
+
* - Redirects unauthenticated users to sign in
|
|
307
|
+
* - Adds user info to request headers for downstream use
|
|
308
|
+
*
|
|
309
|
+
* Note: The `config` export must be defined statically in your proxy.ts file
|
|
310
|
+
* for Next.js to analyze it at compile time.
|
|
311
|
+
*
|
|
312
|
+
* @param options - Proxy configuration
|
|
313
|
+
* @returns A Next.js proxy handler function
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* // src/proxy.ts
|
|
318
|
+
* import { createProxyHandler, type ProxyOptions } from "@logickernel/bridge/next"
|
|
319
|
+
*
|
|
320
|
+
* const proxyConfig: ProxyOptions = {
|
|
321
|
+
* basePath: "/aura",
|
|
322
|
+
* protectedRoutes: { "/dashboard": [] },
|
|
323
|
+
* dynamicRoutes: [
|
|
324
|
+
* { pattern: "/[organization_id]/admin", requiredRoles: ["organization.owner"] },
|
|
325
|
+
* ],
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* export const proxy = createProxyHandler(proxyConfig)
|
|
329
|
+
*
|
|
330
|
+
* // Config must be static for Next.js
|
|
331
|
+
* export const config = {
|
|
332
|
+
* matcher: ["/((?!_next/static|_next/image|favicon.ico|public).*)"],
|
|
333
|
+
* }
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
declare function createProxyHandler(options: ProxyOptions): (request: NextRequest) => Promise<Response>;
|
|
337
|
+
/**
|
|
338
|
+
* Default matcher config for proxy
|
|
339
|
+
* Can be used if you don't want to define your own config
|
|
340
|
+
*/
|
|
341
|
+
declare const defaultProxyConfig: {
|
|
342
|
+
matcher: string[];
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
export { type AuthContext, type AuthenticatedHandler, type DynamicRouteMatch, type DynamicRoutePattern, type PageAuthOptions, type PageAuthResult, type ProxyOptions, Session, SessionCookie, type WithAuthOptions, checkPageAuth, createProxyHandler, defaultProxyConfig, getBaseUrl, getSession, getSessionCookie, getSessionFromRequest, getSessionToken, getUserRoles, hasRequiredRole, isSecureCookie, requireAuth, withAuth };
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { b as Session, S as SessionCookie, M as MicroFrontendConfig } from '../types-YvOY9KNP.js';
|
|
2
|
+
export { d as SessionUser } from '../types-YvOY9KNP.js';
|
|
3
|
+
import { NextRequest } from 'next/server';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Kernel Bridge Next.js Session
|
|
7
|
+
* Server-side session utilities for Next.js
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get the session cookie from Next.js cookies
|
|
12
|
+
*/
|
|
13
|
+
declare function getSessionCookie(): Promise<SessionCookie | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Get the session token value from cookies
|
|
16
|
+
*/
|
|
17
|
+
declare function getSessionToken(): Promise<string | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Check if secure cookie is being used
|
|
20
|
+
*/
|
|
21
|
+
declare function isSecureCookie(): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Get the current session from cookies
|
|
24
|
+
*
|
|
25
|
+
* This reads the NextAuth session cookie and decodes the JWT.
|
|
26
|
+
* Use this in Server Components, Server Actions, or API routes.
|
|
27
|
+
*
|
|
28
|
+
* @returns Session object or null if not authenticated
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { getSession } from "@logickernel/bridge/next"
|
|
33
|
+
*
|
|
34
|
+
* export default async function Page() {
|
|
35
|
+
* const session = await getSession()
|
|
36
|
+
* if (!session) {
|
|
37
|
+
* redirect("/auth/signin")
|
|
38
|
+
* }
|
|
39
|
+
* return <div>Hello {session.user.email}</div>
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function getSession(): Promise<Session | null>;
|
|
44
|
+
/**
|
|
45
|
+
* Get the base URL (facade/load balancer) for user-facing links and redirects
|
|
46
|
+
*
|
|
47
|
+
* This is the URL that users access through the facade/load balancer.
|
|
48
|
+
* Use this for:
|
|
49
|
+
* - Sign-in redirect URLs
|
|
50
|
+
* - User-facing links to the kernel
|
|
51
|
+
* - Any URLs that users will see or click
|
|
52
|
+
*/
|
|
53
|
+
declare function getBaseUrl(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Get user roles in an organization
|
|
56
|
+
*
|
|
57
|
+
* Fetches roles from the kernel API using the current session.
|
|
58
|
+
* The user ID is determined from the session token.
|
|
59
|
+
*
|
|
60
|
+
* @param organizationId - Organization ID to fetch roles in
|
|
61
|
+
* @returns Array of role names (empty if error or no roles)
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { getUserRoles } from "@logickernel/bridge/next"
|
|
66
|
+
*
|
|
67
|
+
* const roles = await getUserRoles("org-123")
|
|
68
|
+
* const isOwner = roles.includes("organization.owner")
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function getUserRoles(organizationId: string): Promise<string[]>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Kernel Bridge Next.js Types
|
|
75
|
+
* Next.js-specific type definitions
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Options for the proxy factory
|
|
80
|
+
*/
|
|
81
|
+
interface ProxyOptions extends MicroFrontendConfig {
|
|
82
|
+
/**
|
|
83
|
+
* Dynamic route patterns that contain organization IDs
|
|
84
|
+
* e.g., "/[organization_id]/admin" matches "/abc123/admin"
|
|
85
|
+
*/
|
|
86
|
+
dynamicRoutes?: DynamicRoutePattern[];
|
|
87
|
+
/**
|
|
88
|
+
* Custom error page path (defaults to showing error in redirect)
|
|
89
|
+
*/
|
|
90
|
+
errorPath?: string;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Dynamic route pattern configuration
|
|
94
|
+
*/
|
|
95
|
+
interface DynamicRoutePattern {
|
|
96
|
+
/**
|
|
97
|
+
* Pattern to match (e.g., "/[organization_id]/admin")
|
|
98
|
+
* Use [param] syntax for dynamic segments
|
|
99
|
+
*/
|
|
100
|
+
pattern: string;
|
|
101
|
+
/**
|
|
102
|
+
* Required roles for this route (empty = any authenticated user)
|
|
103
|
+
*/
|
|
104
|
+
requiredRoles: string[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Result from parsing a dynamic route
|
|
108
|
+
*/
|
|
109
|
+
interface DynamicRouteMatch {
|
|
110
|
+
matched: boolean;
|
|
111
|
+
params: Record<string, string>;
|
|
112
|
+
requiredRoles: string[];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* API route handler with auth context
|
|
116
|
+
*/
|
|
117
|
+
type AuthenticatedHandler = (request: NextRequest, context: AuthContext) => Promise<Response> | Response;
|
|
118
|
+
/**
|
|
119
|
+
* Context provided to authenticated handlers
|
|
120
|
+
*/
|
|
121
|
+
interface AuthContext {
|
|
122
|
+
session: Session;
|
|
123
|
+
params?: Record<string, string>;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Options for withAuth wrapper
|
|
127
|
+
*/
|
|
128
|
+
interface WithAuthOptions {
|
|
129
|
+
/**
|
|
130
|
+
* Required roles (empty = any authenticated user)
|
|
131
|
+
*/
|
|
132
|
+
requiredRoles?: string[];
|
|
133
|
+
/**
|
|
134
|
+
* Organization ID parameter name in dynamic routes
|
|
135
|
+
*/
|
|
136
|
+
organizationId?: string;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Page auth result
|
|
140
|
+
*/
|
|
141
|
+
type PageAuthResult = {
|
|
142
|
+
authenticated: true;
|
|
143
|
+
session: Session;
|
|
144
|
+
roles: string[];
|
|
145
|
+
} | {
|
|
146
|
+
authenticated: false;
|
|
147
|
+
redirectUrl: string;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Options for page auth check
|
|
151
|
+
*/
|
|
152
|
+
interface PageAuthOptions {
|
|
153
|
+
/**
|
|
154
|
+
* Required roles (empty = any authenticated user)
|
|
155
|
+
*/
|
|
156
|
+
requiredRoles?: string[];
|
|
157
|
+
/**
|
|
158
|
+
* Organization ID for role checking
|
|
159
|
+
*/
|
|
160
|
+
organizationId?: string;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Kernel Bridge Next.js Page
|
|
165
|
+
* Authentication utilities for pages/server components
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Check authentication and optionally roles for a page
|
|
170
|
+
*
|
|
171
|
+
* This is a helper for pages that need both auth and role checking.
|
|
172
|
+
* For simple auth-only checks, use getSession() directly.
|
|
173
|
+
*
|
|
174
|
+
* @param options - Authentication options
|
|
175
|
+
* @returns Auth result with session/roles or redirect URL
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* import { checkPageAuth } from "@logickernel/bridge/next"
|
|
180
|
+
* import { redirect } from "next/navigation"
|
|
181
|
+
*
|
|
182
|
+
* export default async function AdminPage({ params }) {
|
|
183
|
+
* const { organization_id } = await params
|
|
184
|
+
*
|
|
185
|
+
* const auth = await checkPageAuth({
|
|
186
|
+
* requiredRoles: ["organization.owner"],
|
|
187
|
+
* organizationId: organization_id,
|
|
188
|
+
* })
|
|
189
|
+
*
|
|
190
|
+
* if (!auth.authenticated) {
|
|
191
|
+
* redirect(auth.redirectUrl)
|
|
192
|
+
* }
|
|
193
|
+
*
|
|
194
|
+
* return <div>Welcome {auth.session.user.email}</div>
|
|
195
|
+
* }
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
declare function checkPageAuth(options?: PageAuthOptions): Promise<PageAuthResult>;
|
|
199
|
+
/**
|
|
200
|
+
* Require authentication for a page
|
|
201
|
+
*
|
|
202
|
+
* This is a simpler helper that throws if not authenticated.
|
|
203
|
+
* Use with redirect() in the catch block.
|
|
204
|
+
*
|
|
205
|
+
* @returns Session if authenticated
|
|
206
|
+
* @throws RedirectInfo if not authenticated
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* import { requireAuth } from "@logickernel/bridge/next"
|
|
211
|
+
* import { redirect } from "next/navigation"
|
|
212
|
+
*
|
|
213
|
+
* export default async function DashboardPage() {
|
|
214
|
+
* const session = await requireAuth()
|
|
215
|
+
* // If we get here, user is authenticated
|
|
216
|
+
* return <div>Hello {session.user.email}</div>
|
|
217
|
+
* }
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
declare function requireAuth(): Promise<Session>;
|
|
221
|
+
/**
|
|
222
|
+
* Check if user has any of the required roles
|
|
223
|
+
*
|
|
224
|
+
* The user ID is determined from the session token in cookies.
|
|
225
|
+
*
|
|
226
|
+
* @param organizationId - Organization to check roles in
|
|
227
|
+
* @param requiredRoles - Roles to check for
|
|
228
|
+
* @returns True if user has at least one required role
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* const hasAccess = await hasRequiredRole(
|
|
233
|
+
* "org-123",
|
|
234
|
+
* ["organization.owner", "organization.editor"]
|
|
235
|
+
* )
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
declare function hasRequiredRole(organizationId: string, requiredRoles: string[]): Promise<boolean>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Kernel Bridge Next.js API
|
|
242
|
+
* Authentication utilities for API routes
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Wrap an API route handler with authentication
|
|
247
|
+
*
|
|
248
|
+
* This handles JWT validation and optionally role checking.
|
|
249
|
+
* The handler receives the session and can focus on business logic.
|
|
250
|
+
*
|
|
251
|
+
* @param handler - The authenticated handler function
|
|
252
|
+
* @param options - Authentication options
|
|
253
|
+
* @returns A Next.js route handler
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* // Any authenticated user
|
|
258
|
+
* export const GET = withAuth(async (request, { session }) => {
|
|
259
|
+
* return NextResponse.json({ user: session.user })
|
|
260
|
+
* })
|
|
261
|
+
*
|
|
262
|
+
* // With role requirement
|
|
263
|
+
* export const DELETE = withAuth(
|
|
264
|
+
* async (request, { session }) => {
|
|
265
|
+
* return NextResponse.json({ deleted: true })
|
|
266
|
+
* },
|
|
267
|
+
* { requiredRoles: ["organization.owner"] }
|
|
268
|
+
* )
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
declare function withAuth(handler: AuthenticatedHandler, options?: WithAuthOptions): (request: NextRequest, context?: {
|
|
272
|
+
params?: Promise<Record<string, string>>;
|
|
273
|
+
}) => Promise<Response>;
|
|
274
|
+
/**
|
|
275
|
+
* Get session from a request (for manual handling)
|
|
276
|
+
*
|
|
277
|
+
* Use this when you need more control than withAuth provides.
|
|
278
|
+
*
|
|
279
|
+
* @param request - The Next.js request
|
|
280
|
+
* @returns Session or null
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* export async function GET(request: NextRequest) {
|
|
285
|
+
* const session = await getSessionFromRequest(request)
|
|
286
|
+
* if (!session) {
|
|
287
|
+
* return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
288
|
+
* }
|
|
289
|
+
* // Custom logic...
|
|
290
|
+
* }
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
declare function getSessionFromRequest(request: NextRequest): Promise<Session | null>;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Kernel Bridge Next.js Proxy
|
|
297
|
+
* Middleware/proxy utilities for Next.js
|
|
298
|
+
*/
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Create a proxy/middleware handler function for Next.js
|
|
302
|
+
*
|
|
303
|
+
* This creates a fully configured middleware handler that:
|
|
304
|
+
* - Validates authentication for protected routes
|
|
305
|
+
* - Handles dynamic routes with organization IDs
|
|
306
|
+
* - Redirects unauthenticated users to sign in
|
|
307
|
+
* - Adds user info to request headers for downstream use
|
|
308
|
+
*
|
|
309
|
+
* Note: The `config` export must be defined statically in your proxy.ts file
|
|
310
|
+
* for Next.js to analyze it at compile time.
|
|
311
|
+
*
|
|
312
|
+
* @param options - Proxy configuration
|
|
313
|
+
* @returns A Next.js proxy handler function
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* // src/proxy.ts
|
|
318
|
+
* import { createProxyHandler, type ProxyOptions } from "@logickernel/bridge/next"
|
|
319
|
+
*
|
|
320
|
+
* const proxyConfig: ProxyOptions = {
|
|
321
|
+
* basePath: "/aura",
|
|
322
|
+
* protectedRoutes: { "/dashboard": [] },
|
|
323
|
+
* dynamicRoutes: [
|
|
324
|
+
* { pattern: "/[organization_id]/admin", requiredRoles: ["organization.owner"] },
|
|
325
|
+
* ],
|
|
326
|
+
* }
|
|
327
|
+
*
|
|
328
|
+
* export const proxy = createProxyHandler(proxyConfig)
|
|
329
|
+
*
|
|
330
|
+
* // Config must be static for Next.js
|
|
331
|
+
* export const config = {
|
|
332
|
+
* matcher: ["/((?!_next/static|_next/image|favicon.ico|public).*)"],
|
|
333
|
+
* }
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
declare function createProxyHandler(options: ProxyOptions): (request: NextRequest) => Promise<Response>;
|
|
337
|
+
/**
|
|
338
|
+
* Default matcher config for proxy
|
|
339
|
+
* Can be used if you don't want to define your own config
|
|
340
|
+
*/
|
|
341
|
+
declare const defaultProxyConfig: {
|
|
342
|
+
matcher: string[];
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
export { type AuthContext, type AuthenticatedHandler, type DynamicRouteMatch, type DynamicRoutePattern, type PageAuthOptions, type PageAuthResult, type ProxyOptions, Session, SessionCookie, type WithAuthOptions, checkPageAuth, createProxyHandler, defaultProxyConfig, getBaseUrl, getSession, getSessionCookie, getSessionFromRequest, getSessionToken, getUserRoles, hasRequiredRole, isSecureCookie, requireAuth, withAuth };
|