@imtbl/auth-nextjs 2.12.5-alpha.7 → 2.12.5-alpha.9

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.
@@ -1,9 +1,3 @@
1
- // src/index.ts
2
- import NextAuthImport from "next-auth";
3
-
4
- // src/config.ts
5
- import CredentialsImport from "next-auth/providers/credentials";
6
-
7
1
  // src/constants.ts
8
2
  var DEFAULT_AUTH_DOMAIN = "https://auth.immutable.com";
9
3
  var DEFAULT_AUDIENCE = "platform_api";
@@ -24,6 +18,7 @@ function isTokenExpired(accessTokenExpires, bufferSeconds = TOKEN_EXPIRY_BUFFER_
24
18
  }
25
19
 
26
20
  // src/config.ts
21
+ import CredentialsImport from "next-auth/providers/credentials";
27
22
  var Credentials = CredentialsImport.default || CredentialsImport;
28
23
  async function validateTokens(accessToken, authDomain) {
29
24
  try {
@@ -164,9 +159,10 @@ function createAuthConfig(config) {
164
159
  }
165
160
  };
166
161
  }
162
+ var createAuthOptions = createAuthConfig;
167
163
 
168
- // src/index.ts
169
- import { MarketingConsentStatus } from "@imtbl/auth";
164
+ // src/createAuth.ts
165
+ import NextAuthImport from "next-auth";
170
166
  var NextAuth = NextAuthImport.default || NextAuthImport;
171
167
  function createImmutableAuth(config, overrides) {
172
168
  const authConfig = createAuthConfig(config);
@@ -226,7 +222,7 @@ export {
226
222
  DEFAULT_NEXTAUTH_BASE_PATH,
227
223
  isTokenExpired,
228
224
  createAuthConfig,
225
+ createAuthOptions,
229
226
  createImmutableAuth,
230
- ImmutableAuth,
231
- MarketingConsentStatus
227
+ ImmutableAuth
232
228
  };
@@ -278,6 +278,7 @@ function useHydratedData(props, fetcher) {
278
278
  fetchError ? new Error(fetchError) : null
279
279
  );
280
280
  const hasFetchedRef = (0, import_react.useRef)(false);
281
+ const fetchIdRef = (0, import_react.useRef)(0);
281
282
  const prevPropsRef = (0, import_react.useRef)({ serverData, ssr, fetchError });
282
283
  (0, import_react.useEffect)(() => {
283
284
  const prevProps = prevPropsRef.current;
@@ -285,6 +286,7 @@ function useHydratedData(props, fetcher) {
285
286
  if (propsChanged) {
286
287
  prevPropsRef.current = { serverData, ssr, fetchError };
287
288
  hasFetchedRef.current = false;
289
+ fetchIdRef.current += 1;
288
290
  if (ssr && !fetchError) {
289
291
  setData(serverData);
290
292
  setIsLoading(false);
@@ -297,16 +299,23 @@ function useHydratedData(props, fetcher) {
297
299
  }
298
300
  }, [serverData, ssr, fetchError]);
299
301
  const fetchData = (0, import_react.useCallback)(async () => {
302
+ const currentFetchId = fetchIdRef.current;
300
303
  setIsLoading(true);
301
304
  setError(null);
302
305
  try {
303
306
  const token = await getAccessToken();
304
307
  const result = await fetcher(token);
305
- setData(result);
308
+ if (fetchIdRef.current === currentFetchId) {
309
+ setData(result);
310
+ }
306
311
  } catch (err) {
307
- setError(err instanceof Error ? err : new Error(String(err)));
312
+ if (fetchIdRef.current === currentFetchId) {
313
+ setError(err instanceof Error ? err : new Error(String(err)));
314
+ }
308
315
  } finally {
309
- setIsLoading(false);
316
+ if (fetchIdRef.current === currentFetchId) {
317
+ setIsLoading(false);
318
+ }
310
319
  }
311
320
  }, [fetcher, getAccessToken]);
312
321
  (0, import_react.useEffect)(() => {
@@ -264,6 +264,7 @@ function useHydratedData(props, fetcher) {
264
264
  fetchError ? new Error(fetchError) : null
265
265
  );
266
266
  const hasFetchedRef = useRef(false);
267
+ const fetchIdRef = useRef(0);
267
268
  const prevPropsRef = useRef({ serverData, ssr, fetchError });
268
269
  useEffect(() => {
269
270
  const prevProps = prevPropsRef.current;
@@ -271,6 +272,7 @@ function useHydratedData(props, fetcher) {
271
272
  if (propsChanged) {
272
273
  prevPropsRef.current = { serverData, ssr, fetchError };
273
274
  hasFetchedRef.current = false;
275
+ fetchIdRef.current += 1;
274
276
  if (ssr && !fetchError) {
275
277
  setData(serverData);
276
278
  setIsLoading(false);
@@ -283,16 +285,23 @@ function useHydratedData(props, fetcher) {
283
285
  }
284
286
  }, [serverData, ssr, fetchError]);
285
287
  const fetchData = useCallback(async () => {
288
+ const currentFetchId = fetchIdRef.current;
286
289
  setIsLoading(true);
287
290
  setError(null);
288
291
  try {
289
292
  const token = await getAccessToken();
290
293
  const result = await fetcher(token);
291
- setData(result);
294
+ if (fetchIdRef.current === currentFetchId) {
295
+ setData(result);
296
+ }
292
297
  } catch (err) {
293
- setError(err instanceof Error ? err : new Error(String(err)));
298
+ if (fetchIdRef.current === currentFetchId) {
299
+ setError(err instanceof Error ? err : new Error(String(err)));
300
+ }
294
301
  } finally {
295
- setIsLoading(false);
302
+ if (fetchIdRef.current === currentFetchId) {
303
+ setIsLoading(false);
304
+ }
296
305
  }
297
306
  }, [fetcher, getAccessToken]);
298
307
  useEffect(() => {
@@ -41,6 +41,8 @@ __export(src_exports, {
41
41
  isTokenExpired: () => isTokenExpired
42
42
  });
43
43
  module.exports = __toCommonJS(src_exports);
44
+
45
+ // src/createAuth.ts
44
46
  var import_next_auth = __toESM(require("next-auth"), 1);
45
47
 
46
48
  // src/config.ts
@@ -207,8 +209,7 @@ function createAuthConfig(config) {
207
209
  };
208
210
  }
209
211
 
210
- // src/index.ts
211
- var import_auth = require("@imtbl/auth");
212
+ // src/createAuth.ts
212
213
  var NextAuth = import_next_auth.default.default || import_next_auth.default;
213
214
  function createImmutableAuth(config, overrides) {
214
215
  const authConfig = createAuthConfig(config);
@@ -260,6 +261,9 @@ function createImmutableAuth(config, overrides) {
260
261
  return NextAuth(mergedConfig);
261
262
  }
262
263
  var ImmutableAuth = createImmutableAuth;
264
+
265
+ // src/index.ts
266
+ var import_auth = require("@imtbl/auth");
263
267
  // Annotate the CommonJS export names for ESM import in node:
264
268
  0 && (module.exports = {
265
269
  DEFAULT_AUDIENCE,
@@ -4,11 +4,13 @@ import {
4
4
  DEFAULT_NEXTAUTH_BASE_PATH,
5
5
  DEFAULT_SCOPE,
6
6
  ImmutableAuth,
7
- MarketingConsentStatus,
8
7
  createAuthConfig,
9
8
  createImmutableAuth,
10
9
  isTokenExpired
11
- } from "./chunk-JNVY7OLV.js";
10
+ } from "./chunk-ZJXQWSJZ.js";
11
+
12
+ // src/index.ts
13
+ import { MarketingConsentStatus } from "@imtbl/auth";
12
14
  export {
13
15
  DEFAULT_AUDIENCE,
14
16
  DEFAULT_AUTH_DOMAIN,
@@ -32,6 +32,7 @@ var server_exports = {};
32
32
  __export(server_exports, {
33
33
  createAuthConfig: () => createAuthConfig,
34
34
  createAuthMiddleware: () => createAuthMiddleware,
35
+ createAuthOptions: () => createAuthOptions,
35
36
  createImmutableAuth: () => createImmutableAuth,
36
37
  createProtectedAuthProps: () => createProtectedAuthProps,
37
38
  createProtectedDataFetcher: () => createProtectedDataFetcher,
@@ -52,7 +53,7 @@ function matchPathPrefix(pathname, pattern) {
52
53
  return pathname.startsWith(prefix);
53
54
  }
54
55
 
55
- // src/index.ts
56
+ // src/createAuth.ts
56
57
  var import_next_auth = __toESM(require("next-auth"), 1);
57
58
 
58
59
  // src/config.ts
@@ -215,9 +216,9 @@ function createAuthConfig(config) {
215
216
  }
216
217
  };
217
218
  }
219
+ var createAuthOptions = createAuthConfig;
218
220
 
219
- // src/index.ts
220
- var import_auth = require("@imtbl/auth");
221
+ // src/createAuth.ts
221
222
  var NextAuth = import_next_auth.default.default || import_next_auth.default;
222
223
  function createImmutableAuth(config, overrides) {
223
224
  const authConfig = createAuthConfig(config);
@@ -434,6 +435,7 @@ function withAuth(auth, handler) {
434
435
  0 && (module.exports = {
435
436
  createAuthConfig,
436
437
  createAuthMiddleware,
438
+ createAuthOptions,
437
439
  createImmutableAuth,
438
440
  createProtectedAuthProps,
439
441
  createProtectedDataFetcher,
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  createAuthConfig,
3
+ createAuthOptions,
3
4
  createImmutableAuth
4
- } from "../chunk-JNVY7OLV.js";
5
+ } from "../chunk-ZJXQWSJZ.js";
5
6
 
6
7
  // src/server/index.ts
7
8
  import { NextResponse } from "next/server";
@@ -177,6 +178,7 @@ function withAuth(auth, handler) {
177
178
  export {
178
179
  createAuthConfig,
179
180
  createAuthMiddleware,
181
+ createAuthOptions,
180
182
  createImmutableAuth,
181
183
  createProtectedAuthProps,
182
184
  createProtectedDataFetcher,
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Server-safe auth creation module.
3
+ *
4
+ * This file contains createImmutableAuth and createAuthConfig without importing
5
+ * from @imtbl/auth to avoid loading browser-only dependencies (metrics, oidc-client-ts)
6
+ * in Edge Runtime or server-side environments.
7
+ *
8
+ * For client-side usage with full @imtbl/auth types, use the main index.ts instead.
9
+ */
10
+ import NextAuthImport from 'next-auth';
11
+ import type { NextAuthConfig } from 'next-auth';
12
+ import type { ImmutableAuthConfig } from './types';
13
+ declare const NextAuth: typeof NextAuthImport;
14
+ /**
15
+ * Auth.js v5 config options that can be overridden.
16
+ * Excludes 'providers' as that's managed internally.
17
+ */
18
+ export type ImmutableAuthOverrides = Omit<NextAuthConfig, 'providers'>;
19
+ /**
20
+ * Return type of createImmutableAuth - the NextAuth instance with handlers
21
+ */
22
+ export type ImmutableAuthResult = ReturnType<typeof NextAuth>;
23
+ /**
24
+ * Create an Auth.js v5 instance with Immutable authentication
25
+ *
26
+ * @param config - Immutable auth configuration
27
+ * @param overrides - Optional Auth.js options to override defaults
28
+ * @returns NextAuth instance with { handlers, auth, signIn, signOut }
29
+ *
30
+ * @remarks
31
+ * Callback composition: The `jwt` and `session` callbacks are composed rather than
32
+ * replaced. Internal callbacks run first (handling token storage and refresh), then
33
+ * your custom callbacks receive the result. Other callbacks (`signIn`, `redirect`)
34
+ * are replaced entirely if provided.
35
+ *
36
+ * @example Basic usage (App Router)
37
+ * ```typescript
38
+ * // lib/auth.ts
39
+ * import { createImmutableAuth } from "@imtbl/auth-nextjs";
40
+ *
41
+ * export const { handlers, auth, signIn, signOut } = createImmutableAuth({
42
+ * clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
43
+ * redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
44
+ * });
45
+ *
46
+ * // app/api/auth/[...nextauth]/route.ts
47
+ * import { handlers } from "@/lib/auth";
48
+ * export const { GET, POST } = handlers;
49
+ * ```
50
+ *
51
+ * @example With Auth.js overrides
52
+ * ```typescript
53
+ * export const { handlers, auth } = createImmutableAuth(
54
+ * { clientId: "...", redirectUri: "..." },
55
+ * {
56
+ * pages: { signIn: "/custom-login", error: "/auth-error" },
57
+ * debug: true,
58
+ * }
59
+ * );
60
+ * ```
61
+ *
62
+ * @example With custom jwt callback (composed with internal callback)
63
+ * ```typescript
64
+ * export const { handlers, auth } = createImmutableAuth(
65
+ * { clientId: "...", redirectUri: "..." },
66
+ * {
67
+ * callbacks: {
68
+ * // Your jwt callback receives the token after internal processing
69
+ * async jwt({ token }) {
70
+ * // Add custom claims
71
+ * token.customClaim = "value";
72
+ * return token;
73
+ * },
74
+ * },
75
+ * }
76
+ * );
77
+ * ```
78
+ */
79
+ export declare function createImmutableAuth(config: ImmutableAuthConfig, overrides?: ImmutableAuthOverrides): ImmutableAuthResult;
80
+ export declare const ImmutableAuth: typeof createImmutableAuth;
81
+ export { createAuthConfig } from './config';
@@ -1,74 +1,4 @@
1
- import NextAuthImport from 'next-auth';
2
- import type { NextAuthConfig } from 'next-auth';
3
- import type { ImmutableAuthConfig } from './types';
4
- declare const NextAuth: typeof NextAuthImport;
5
- /**
6
- * Auth.js v5 config options that can be overridden.
7
- * Excludes 'providers' as that's managed internally.
8
- */
9
- export type ImmutableAuthOverrides = Omit<NextAuthConfig, 'providers'>;
10
- /**
11
- * Return type of createImmutableAuth - the NextAuth instance with handlers
12
- */
13
- export type ImmutableAuthResult = ReturnType<typeof NextAuth>;
14
- /**
15
- * Create an Auth.js v5 instance with Immutable authentication
16
- *
17
- * @param config - Immutable auth configuration
18
- * @param overrides - Optional Auth.js options to override defaults
19
- * @returns NextAuth instance with { handlers, auth, signIn, signOut }
20
- *
21
- * @remarks
22
- * Callback composition: The `jwt` and `session` callbacks are composed rather than
23
- * replaced. Internal callbacks run first (handling token storage and refresh), then
24
- * your custom callbacks receive the result. Other callbacks (`signIn`, `redirect`)
25
- * are replaced entirely if provided.
26
- *
27
- * @example Basic usage (App Router)
28
- * ```typescript
29
- * // lib/auth.ts
30
- * import { createImmutableAuth } from "@imtbl/auth-nextjs";
31
- *
32
- * export const { handlers, auth, signIn, signOut } = createImmutableAuth({
33
- * clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
34
- * redirectUri: `${process.env.NEXT_PUBLIC_BASE_URL}/callback`,
35
- * });
36
- *
37
- * // app/api/auth/[...nextauth]/route.ts
38
- * import { handlers } from "@/lib/auth";
39
- * export const { GET, POST } = handlers;
40
- * ```
41
- *
42
- * @example With Auth.js overrides
43
- * ```typescript
44
- * export const { handlers, auth } = createImmutableAuth(
45
- * { clientId: "...", redirectUri: "..." },
46
- * {
47
- * pages: { signIn: "/custom-login", error: "/auth-error" },
48
- * debug: true,
49
- * }
50
- * );
51
- * ```
52
- *
53
- * @example With custom jwt callback (composed with internal callback)
54
- * ```typescript
55
- * export const { handlers, auth } = createImmutableAuth(
56
- * { clientId: "...", redirectUri: "..." },
57
- * {
58
- * callbacks: {
59
- * // Your jwt callback receives the token after internal processing
60
- * async jwt({ token }) {
61
- * // Add custom claims
62
- * token.customClaim = "value";
63
- * return token;
64
- * },
65
- * },
66
- * }
67
- * );
68
- * ```
69
- */
70
- export declare function createImmutableAuth(config: ImmutableAuthConfig, overrides?: ImmutableAuthOverrides): ImmutableAuthResult;
71
- export declare const ImmutableAuth: typeof createImmutableAuth;
1
+ export { createImmutableAuth, ImmutableAuth, type ImmutableAuthOverrides, type ImmutableAuthResult, } from './createAuth';
72
2
  export { createAuthConfig } from './config';
73
3
  export type { ImmutableAuthConfig, ImmutableTokenData, ImmutableUser, ZkEvmInfo, } from './types';
74
4
  export type { LoginOptions, DirectLoginOptions } from '@imtbl/auth';
@@ -1,6 +1,7 @@
1
1
  import { type NextRequest, NextResponse } from 'next/server';
2
2
  import type { Session } from 'next-auth';
3
- export { createImmutableAuth, createAuthConfig } from '../index';
3
+ export { createImmutableAuth } from '../createAuth';
4
+ export { createAuthConfig, createAuthOptions } from '../config';
4
5
  /**
5
6
  * Result from getValidSession indicating auth state
6
7
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imtbl/auth-nextjs",
3
- "version": "2.12.5-alpha.7",
3
+ "version": "2.12.5-alpha.9",
4
4
  "description": "Next.js App Router authentication integration for Immutable SDK using Auth.js v5",
5
5
  "author": "Immutable",
6
6
  "bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
@@ -51,12 +51,12 @@
51
51
  "dist"
52
52
  ],
53
53
  "dependencies": {
54
- "@imtbl/auth": "2.12.5-alpha.7"
54
+ "@imtbl/auth": "2.12.5-alpha.9"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "next": "^14.2.0 || ^15.0.0",
58
58
  "next-auth": "^5.0.0-beta.25",
59
- "react": "^18.2.0"
59
+ "react": "^18.2.0 || ^19.0.0"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@swc/core": "^1.3.36",