@imtbl/auth-next-server 2.12.5-alpha.21 → 2.12.6-alpha.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 +0 -48
- package/dist/node/index.cjs +2 -28
- package/dist/node/index.js +1 -26
- package/dist/types/index.d.ts +0 -31
- package/dist/types/refresh.d.ts +2 -4
- package/dist/types/types.d.ts +5 -12
- package/package.json +1 -1
- package/src/index.ts +0 -74
- package/src/refresh.ts +2 -4
- package/src/types.ts +5 -11
package/README.md
CHANGED
|
@@ -147,7 +147,6 @@ This package provides several utilities for handling authentication in Server Co
|
|
|
147
147
|
| `getAuthenticatedData` | SSR data fetching with client fallback | Yes | Manual |
|
|
148
148
|
| `createProtectedFetchers` | Multiple pages with same error handling | Optional | Centralized |
|
|
149
149
|
| `getValidSession` | Custom logic for each auth state | No | Manual (detailed) |
|
|
150
|
-
| `withServerAuth` | Render different JSX per auth state | Yes | Via callbacks |
|
|
151
150
|
|
|
152
151
|
### `getAuthProps(auth)`
|
|
153
152
|
|
|
@@ -318,53 +317,6 @@ export default async function AccountPage() {
|
|
|
318
317
|
}
|
|
319
318
|
```
|
|
320
319
|
|
|
321
|
-
### `withServerAuth(auth, serverRender, options)`
|
|
322
|
-
|
|
323
|
-
**Use case:** You want to render a Server Component with authenticated data, but declaratively specify fallback components for different auth states.
|
|
324
|
-
|
|
325
|
-
**When to use:**
|
|
326
|
-
- Server Components that fetch and render data in one place
|
|
327
|
-
- When you prefer a declarative, callback-based API over imperative if/else
|
|
328
|
-
- Pages where the fallback UI is a different component (not just a redirect)
|
|
329
|
-
|
|
330
|
-
**Comparison with `getAuthenticatedData`:**
|
|
331
|
-
- `getAuthenticatedData` returns data for you to pass to a Client Component
|
|
332
|
-
- `withServerAuth` lets you render JSX directly in the Server Component
|
|
333
|
-
|
|
334
|
-
```typescript
|
|
335
|
-
// app/inventory/page.tsx
|
|
336
|
-
// Use case: Server Component that renders inventory with fallback components
|
|
337
|
-
import { auth } from "@/lib/auth";
|
|
338
|
-
import { withServerAuth } from "@imtbl/auth-next-server";
|
|
339
|
-
import { redirect } from "next/navigation";
|
|
340
|
-
|
|
341
|
-
export default async function InventoryPage() {
|
|
342
|
-
return withServerAuth(
|
|
343
|
-
auth,
|
|
344
|
-
// This runs when token is valid - fetch and render server-side
|
|
345
|
-
async (session) => {
|
|
346
|
-
const inventory = await fetchInventory(session.accessToken);
|
|
347
|
-
return (
|
|
348
|
-
<div>
|
|
349
|
-
<h1>Your Inventory</h1>
|
|
350
|
-
<InventoryGrid items={inventory} />
|
|
351
|
-
</div>
|
|
352
|
-
);
|
|
353
|
-
},
|
|
354
|
-
{
|
|
355
|
-
// Token expired - render a client component that will refresh and fetch
|
|
356
|
-
onTokenExpired: <InventoryClientFallback />,
|
|
357
|
-
|
|
358
|
-
// Not authenticated - redirect to login
|
|
359
|
-
onUnauthenticated: () => redirect("/login"),
|
|
360
|
-
|
|
361
|
-
// Auth error - redirect with error message
|
|
362
|
-
onError: (error) => redirect(`/login?error=${error}`),
|
|
363
|
-
}
|
|
364
|
-
);
|
|
365
|
-
}
|
|
366
|
-
```
|
|
367
|
-
|
|
368
320
|
## Middleware
|
|
369
321
|
|
|
370
322
|
### `createAuthMiddleware(auth, options)`
|
package/dist/node/index.cjs
CHANGED
|
@@ -43,8 +43,7 @@ __export(src_exports, {
|
|
|
43
43
|
getValidSession: () => getValidSession,
|
|
44
44
|
isTokenExpired: () => isTokenExpired,
|
|
45
45
|
refreshAccessToken: () => refreshAccessToken,
|
|
46
|
-
withAuth: () => withAuth
|
|
47
|
-
withServerAuth: () => withServerAuth
|
|
46
|
+
withAuth: () => withAuth
|
|
48
47
|
});
|
|
49
48
|
module.exports = __toCommonJS(src_exports);
|
|
50
49
|
var import_server = require("next/server");
|
|
@@ -425,30 +424,6 @@ function createProtectedFetchers(auth, onAuthError) {
|
|
|
425
424
|
getData: createProtectedDataFetcher(auth, onAuthError)
|
|
426
425
|
};
|
|
427
426
|
}
|
|
428
|
-
async function withServerAuth(auth, serverRender, options = {}) {
|
|
429
|
-
const result = await getValidSession(auth);
|
|
430
|
-
switch (result.status) {
|
|
431
|
-
case "authenticated":
|
|
432
|
-
return serverRender(result.session);
|
|
433
|
-
case "token_expired":
|
|
434
|
-
if (options.onTokenExpired !== void 0) {
|
|
435
|
-
return typeof options.onTokenExpired === "function" ? options.onTokenExpired() : options.onTokenExpired;
|
|
436
|
-
}
|
|
437
|
-
return serverRender(result.session);
|
|
438
|
-
case "unauthenticated":
|
|
439
|
-
if (options.onUnauthenticated !== void 0) {
|
|
440
|
-
return typeof options.onUnauthenticated === "function" ? options.onUnauthenticated() : options.onUnauthenticated;
|
|
441
|
-
}
|
|
442
|
-
throw new Error("Unauthorized: No active session");
|
|
443
|
-
case "error":
|
|
444
|
-
if (options.onError !== void 0) {
|
|
445
|
-
return typeof options.onError === "function" ? options.onError(result.error) : options.onError;
|
|
446
|
-
}
|
|
447
|
-
throw new Error(`Unauthorized: ${result.error}`);
|
|
448
|
-
default:
|
|
449
|
-
throw new Error("Unknown auth state");
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
427
|
function createAuthMiddleware(auth, options = {}) {
|
|
453
428
|
const { loginUrl = "/login", protectedPaths, publicPaths } = options;
|
|
454
429
|
return async function middleware(request) {
|
|
@@ -519,6 +494,5 @@ function withAuth(auth, handler) {
|
|
|
519
494
|
getValidSession,
|
|
520
495
|
isTokenExpired,
|
|
521
496
|
refreshAccessToken,
|
|
522
|
-
withAuth
|
|
523
|
-
withServerAuth
|
|
497
|
+
withAuth
|
|
524
498
|
});
|
package/dist/node/index.js
CHANGED
|
@@ -377,30 +377,6 @@ function createProtectedFetchers(auth, onAuthError) {
|
|
|
377
377
|
getData: createProtectedDataFetcher(auth, onAuthError)
|
|
378
378
|
};
|
|
379
379
|
}
|
|
380
|
-
async function withServerAuth(auth, serverRender, options = {}) {
|
|
381
|
-
const result = await getValidSession(auth);
|
|
382
|
-
switch (result.status) {
|
|
383
|
-
case "authenticated":
|
|
384
|
-
return serverRender(result.session);
|
|
385
|
-
case "token_expired":
|
|
386
|
-
if (options.onTokenExpired !== void 0) {
|
|
387
|
-
return typeof options.onTokenExpired === "function" ? options.onTokenExpired() : options.onTokenExpired;
|
|
388
|
-
}
|
|
389
|
-
return serverRender(result.session);
|
|
390
|
-
case "unauthenticated":
|
|
391
|
-
if (options.onUnauthenticated !== void 0) {
|
|
392
|
-
return typeof options.onUnauthenticated === "function" ? options.onUnauthenticated() : options.onUnauthenticated;
|
|
393
|
-
}
|
|
394
|
-
throw new Error("Unauthorized: No active session");
|
|
395
|
-
case "error":
|
|
396
|
-
if (options.onError !== void 0) {
|
|
397
|
-
return typeof options.onError === "function" ? options.onError(result.error) : options.onError;
|
|
398
|
-
}
|
|
399
|
-
throw new Error(`Unauthorized: ${result.error}`);
|
|
400
|
-
default:
|
|
401
|
-
throw new Error("Unknown auth state");
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
380
|
function createAuthMiddleware(auth, options = {}) {
|
|
405
381
|
const { loginUrl = "/login", protectedPaths, publicPaths } = options;
|
|
406
382
|
return async function middleware(request) {
|
|
@@ -470,6 +446,5 @@ export {
|
|
|
470
446
|
getValidSession,
|
|
471
447
|
isTokenExpired,
|
|
472
448
|
refreshAccessToken,
|
|
473
|
-
withAuth
|
|
474
|
-
withServerAuth
|
|
449
|
+
withAuth
|
|
475
450
|
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -183,37 +183,6 @@ export interface ProtectedFetchers {
|
|
|
183
183
|
* @returns Object with getAuthProps and getData functions
|
|
184
184
|
*/
|
|
185
185
|
export declare function createProtectedFetchers(auth: AuthFunction, onAuthError: AuthErrorHandler): ProtectedFetchers;
|
|
186
|
-
/**
|
|
187
|
-
* Options for withServerAuth
|
|
188
|
-
*/
|
|
189
|
-
export interface WithServerAuthOptions<TFallback> {
|
|
190
|
-
/**
|
|
191
|
-
* Content to render when token is expired.
|
|
192
|
-
* This should typically be a Client Component that will refresh tokens and fetch data.
|
|
193
|
-
* If not provided, the serverRender function will still be called with the expired session.
|
|
194
|
-
*/
|
|
195
|
-
onTokenExpired?: TFallback | (() => TFallback);
|
|
196
|
-
/**
|
|
197
|
-
* Content to render when user is not authenticated at all.
|
|
198
|
-
* If not provided, throws an error.
|
|
199
|
-
*/
|
|
200
|
-
onUnauthenticated?: TFallback | (() => TFallback);
|
|
201
|
-
/**
|
|
202
|
-
* Content to render when there's an auth error (e.g., refresh token invalid).
|
|
203
|
-
* If not provided, throws an error.
|
|
204
|
-
*/
|
|
205
|
-
onError?: TFallback | ((error: string) => TFallback);
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Helper for Server Components that need authenticated data.
|
|
209
|
-
* Automatically handles token expiration by rendering a client fallback.
|
|
210
|
-
*
|
|
211
|
-
* @param auth - The auth function from NextAuth(createAuthConfig(...))
|
|
212
|
-
* @param serverRender - Async function that receives valid session and returns JSX
|
|
213
|
-
* @param options - Fallback options for different auth states
|
|
214
|
-
* @returns The rendered content based on auth state
|
|
215
|
-
*/
|
|
216
|
-
export declare function withServerAuth<TResult, TFallback = TResult>(auth: AuthFunction, serverRender: (session: Session) => Promise<TResult>, options?: WithServerAuthOptions<TFallback>): Promise<TResult | TFallback>;
|
|
217
186
|
/**
|
|
218
187
|
* Options for createAuthMiddleware
|
|
219
188
|
*/
|
package/dist/types/refresh.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ZkEvmInfo } from './types';
|
|
1
2
|
/**
|
|
2
3
|
* Check if the access token is expired or about to expire
|
|
3
4
|
* Returns true if token expires within the buffer time (default 60 seconds)
|
|
@@ -19,10 +20,7 @@ export interface RefreshedTokens {
|
|
|
19
20
|
/**
|
|
20
21
|
* zkEvm user data extracted from the ID token
|
|
21
22
|
*/
|
|
22
|
-
export
|
|
23
|
-
ethAddress: string;
|
|
24
|
-
userAdminAddress: string;
|
|
25
|
-
}
|
|
23
|
+
export type ZkEvmData = ZkEvmInfo;
|
|
26
24
|
/**
|
|
27
25
|
* Extract zkEvm claims from an ID token.
|
|
28
26
|
* The ID token contains zkEvm data in the `passport` claim after user registration.
|
package/dist/types/types.d.ts
CHANGED
|
@@ -5,9 +5,9 @@ import type { DefaultSession } from 'next-auth';
|
|
|
5
5
|
/**
|
|
6
6
|
* zkEVM wallet information for module augmentation
|
|
7
7
|
*/
|
|
8
|
-
interface ZkEvmInfo {
|
|
9
|
-
ethAddress: string
|
|
10
|
-
userAdminAddress: string
|
|
8
|
+
export interface ZkEvmInfo {
|
|
9
|
+
ethAddress: `0x${string}`;
|
|
10
|
+
userAdminAddress: `0x${string}`;
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* Auth.js v5 module augmentation to add Immutable-specific fields
|
|
@@ -77,10 +77,7 @@ export interface ImmutableTokenData {
|
|
|
77
77
|
email?: string;
|
|
78
78
|
nickname?: string;
|
|
79
79
|
};
|
|
80
|
-
zkEvm?:
|
|
81
|
-
ethAddress: string;
|
|
82
|
-
userAdminAddress: string;
|
|
83
|
-
};
|
|
80
|
+
zkEvm?: ZkEvmInfo;
|
|
84
81
|
}
|
|
85
82
|
/**
|
|
86
83
|
* Response from the userinfo endpoint
|
|
@@ -95,10 +92,7 @@ export interface UserInfoResponse {
|
|
|
95
92
|
/**
|
|
96
93
|
* zkEVM user data stored in session
|
|
97
94
|
*/
|
|
98
|
-
export
|
|
99
|
-
ethAddress: string;
|
|
100
|
-
userAdminAddress: string;
|
|
101
|
-
}
|
|
95
|
+
export type ZkEvmUser = ZkEvmInfo;
|
|
102
96
|
/**
|
|
103
97
|
* Immutable user data structure
|
|
104
98
|
*/
|
|
@@ -108,4 +102,3 @@ export interface ImmutableUser {
|
|
|
108
102
|
nickname?: string;
|
|
109
103
|
zkEvm?: ZkEvmUser;
|
|
110
104
|
}
|
|
111
|
-
export {};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -345,80 +345,6 @@ export function createProtectedFetchers(
|
|
|
345
345
|
};
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
-
/**
|
|
349
|
-
* Options for withServerAuth
|
|
350
|
-
*/
|
|
351
|
-
export interface WithServerAuthOptions<TFallback> {
|
|
352
|
-
/**
|
|
353
|
-
* Content to render when token is expired.
|
|
354
|
-
* This should typically be a Client Component that will refresh tokens and fetch data.
|
|
355
|
-
* If not provided, the serverRender function will still be called with the expired session.
|
|
356
|
-
*/
|
|
357
|
-
onTokenExpired?: TFallback | (() => TFallback);
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* Content to render when user is not authenticated at all.
|
|
361
|
-
* If not provided, throws an error.
|
|
362
|
-
*/
|
|
363
|
-
onUnauthenticated?: TFallback | (() => TFallback);
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Content to render when there's an auth error (e.g., refresh token invalid).
|
|
367
|
-
* If not provided, throws an error.
|
|
368
|
-
*/
|
|
369
|
-
onError?: TFallback | ((error: string) => TFallback);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* Helper for Server Components that need authenticated data.
|
|
374
|
-
* Automatically handles token expiration by rendering a client fallback.
|
|
375
|
-
*
|
|
376
|
-
* @param auth - The auth function from NextAuth(createAuthConfig(...))
|
|
377
|
-
* @param serverRender - Async function that receives valid session and returns JSX
|
|
378
|
-
* @param options - Fallback options for different auth states
|
|
379
|
-
* @returns The rendered content based on auth state
|
|
380
|
-
*/
|
|
381
|
-
export async function withServerAuth<TResult, TFallback = TResult>(
|
|
382
|
-
auth: AuthFunction,
|
|
383
|
-
serverRender: (session: Session) => Promise<TResult>,
|
|
384
|
-
options: WithServerAuthOptions<TFallback> = {},
|
|
385
|
-
): Promise<TResult | TFallback> {
|
|
386
|
-
const result = await getValidSession(auth);
|
|
387
|
-
|
|
388
|
-
switch (result.status) {
|
|
389
|
-
case 'authenticated':
|
|
390
|
-
return serverRender(result.session);
|
|
391
|
-
|
|
392
|
-
case 'token_expired':
|
|
393
|
-
if (options.onTokenExpired !== undefined) {
|
|
394
|
-
return typeof options.onTokenExpired === 'function'
|
|
395
|
-
? (options.onTokenExpired as () => TFallback)()
|
|
396
|
-
: options.onTokenExpired;
|
|
397
|
-
}
|
|
398
|
-
// If no fallback provided, still call serverRender - handler can check session.error
|
|
399
|
-
return serverRender(result.session);
|
|
400
|
-
|
|
401
|
-
case 'unauthenticated':
|
|
402
|
-
if (options.onUnauthenticated !== undefined) {
|
|
403
|
-
return typeof options.onUnauthenticated === 'function'
|
|
404
|
-
? (options.onUnauthenticated as () => TFallback)()
|
|
405
|
-
: options.onUnauthenticated;
|
|
406
|
-
}
|
|
407
|
-
throw new Error('Unauthorized: No active session');
|
|
408
|
-
|
|
409
|
-
case 'error':
|
|
410
|
-
if (options.onError !== undefined) {
|
|
411
|
-
return typeof options.onError === 'function'
|
|
412
|
-
? (options.onError as (error: string) => TFallback)(result.error)
|
|
413
|
-
: options.onError;
|
|
414
|
-
}
|
|
415
|
-
throw new Error(`Unauthorized: ${result.error}`);
|
|
416
|
-
|
|
417
|
-
default:
|
|
418
|
-
throw new Error('Unknown auth state');
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
348
|
// ============================================================================
|
|
423
349
|
// Middleware
|
|
424
350
|
// ============================================================================
|
package/src/refresh.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TOKEN_EXPIRY_BUFFER_SECONDS, DEFAULT_AUTH_DOMAIN } from './constants';
|
|
2
|
+
import type { ZkEvmInfo } from './types';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Check if the access token is expired or about to expire
|
|
@@ -52,10 +53,7 @@ function decodeJwtExpiry(token: string): number {
|
|
|
52
53
|
/**
|
|
53
54
|
* zkEvm user data extracted from the ID token
|
|
54
55
|
*/
|
|
55
|
-
export
|
|
56
|
-
ethAddress: string;
|
|
57
|
-
userAdminAddress: string;
|
|
58
|
-
}
|
|
56
|
+
export type ZkEvmData = ZkEvmInfo;
|
|
59
57
|
|
|
60
58
|
/**
|
|
61
59
|
* Extract zkEvm claims from an ID token.
|
package/src/types.ts
CHANGED
|
@@ -7,9 +7,9 @@ import type { DefaultSession } from 'next-auth';
|
|
|
7
7
|
/**
|
|
8
8
|
* zkEVM wallet information for module augmentation
|
|
9
9
|
*/
|
|
10
|
-
interface ZkEvmInfo {
|
|
11
|
-
ethAddress: string
|
|
12
|
-
userAdminAddress: string
|
|
10
|
+
export interface ZkEvmInfo {
|
|
11
|
+
ethAddress: `0x${string}`;
|
|
12
|
+
userAdminAddress: `0x${string}`;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -88,10 +88,7 @@ export interface ImmutableTokenData {
|
|
|
88
88
|
email?: string;
|
|
89
89
|
nickname?: string;
|
|
90
90
|
};
|
|
91
|
-
zkEvm?:
|
|
92
|
-
ethAddress: string;
|
|
93
|
-
userAdminAddress: string;
|
|
94
|
-
};
|
|
91
|
+
zkEvm?: ZkEvmInfo;
|
|
95
92
|
}
|
|
96
93
|
|
|
97
94
|
/**
|
|
@@ -108,10 +105,7 @@ export interface UserInfoResponse {
|
|
|
108
105
|
/**
|
|
109
106
|
* zkEVM user data stored in session
|
|
110
107
|
*/
|
|
111
|
-
export
|
|
112
|
-
ethAddress: string;
|
|
113
|
-
userAdminAddress: string;
|
|
114
|
-
}
|
|
108
|
+
export type ZkEvmUser = ZkEvmInfo;
|
|
115
109
|
|
|
116
110
|
/**
|
|
117
111
|
* Immutable user data structure
|