@scalemule/nextjs 0.1.74 → 0.1.76

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/dist/index.js CHANGED
@@ -1149,6 +1149,24 @@ function reportSdkError(payload) {
1149
1149
  } catch {
1150
1150
  }
1151
1151
  }
1152
+
1153
+ // src/session-errors.ts
1154
+ var SESSION_ENDED_CODES = /* @__PURE__ */ new Set([
1155
+ "UNAUTHORIZED",
1156
+ "INVALID_SESSION",
1157
+ "SESSION_EXPIRED",
1158
+ "SESSION_IDLE_EXPIRED",
1159
+ "SESSION_ABSOLUTE_EXPIRED",
1160
+ "SESSION_REVOKED",
1161
+ "TOKEN_EXPIRED",
1162
+ "TOKEN_INVALID"
1163
+ ]);
1164
+ function isSessionEndedError(error) {
1165
+ if (!error || typeof error !== "object") return false;
1166
+ const { status, code } = error;
1167
+ if (status === 401) return true;
1168
+ return typeof code === "string" && SESSION_ENDED_CODES.has(code);
1169
+ }
1152
1170
  var USER_CACHE_KEY = "scalemule_user";
1153
1171
  function getCachedUser() {
1154
1172
  if (typeof window === "undefined") return null;
@@ -1170,6 +1188,26 @@ function setCachedUser(user) {
1170
1188
  } catch {
1171
1189
  }
1172
1190
  }
1191
+ var PROXY_SESSION_RETRY_DELAYS_MS = [1e3, 3e3];
1192
+ var PROXY_SESSION_CHECK_TIMEOUT_MS = 1e4;
1193
+ async function checkProxySession(authProxyUrl) {
1194
+ let response;
1195
+ let body;
1196
+ try {
1197
+ const timeout = typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function" ? AbortSignal.timeout(PROXY_SESSION_CHECK_TIMEOUT_MS) : void 0;
1198
+ response = await fetch(`${authProxyUrl}/me`, { credentials: "include", signal: timeout });
1199
+ body = await response.json().catch(() => null);
1200
+ } catch {
1201
+ return { kind: "failed" };
1202
+ }
1203
+ if (body?.success && body.data?.user) {
1204
+ return { kind: "user", data: body.data };
1205
+ }
1206
+ if (response.status === 401 || isSessionEndedError(body?.error) || isSessionEndedError(body?.data?.error) || response.ok && body?.success === true) {
1207
+ return { kind: "ended" };
1208
+ }
1209
+ return { kind: "failed" };
1210
+ }
1173
1211
  var ScaleMuleContext = React.createContext(null);
1174
1212
  function ScaleMuleProvider({
1175
1213
  apiKey,
@@ -1342,17 +1380,25 @@ function ScaleMuleProvider({
1342
1380
  setInitializing(false);
1343
1381
  }
1344
1382
  try {
1345
- const response = await fetch(`${authProxyUrl}/me`, {
1346
- credentials: "include"
1347
- });
1348
- const data = await response.json();
1383
+ let check = await checkProxySession(authProxyUrl);
1384
+ for (const delay of PROXY_SESSION_RETRY_DELAYS_MS) {
1385
+ if (check.kind !== "failed" || !mounted) break;
1386
+ await new Promise((resolve) => setTimeout(resolve, delay));
1387
+ check = await checkProxySession(authProxyUrl);
1388
+ }
1349
1389
  if (mounted) {
1350
- if (data.success && data.data?.user) {
1351
- client.setCookieSession(data.data.user.id);
1352
- setUser(data.data.user);
1353
- setCachedUser(data.data.user);
1354
- if (data.data.sessionToken) {
1355
- await client.setSession(data.data.sessionToken, data.data.userId || "");
1390
+ if (check.kind === "user") {
1391
+ const data = check.data;
1392
+ client.setCookieSession(data.user.id);
1393
+ setUser(data.user);
1394
+ setCachedUser(data.user);
1395
+ if (data.sessionToken) {
1396
+ await client.setSession(data.sessionToken, data.userId || "");
1397
+ }
1398
+ } else if (check.kind === "failed" && cachedUser) {
1399
+ client.setCookieSession(cachedUser.id);
1400
+ if (debug) {
1401
+ console.debug("[ScaleMule] Auth proxy session check failed; keeping the signed-in user");
1356
1402
  }
1357
1403
  } else {
1358
1404
  client.setCookieSession(null);
@@ -1360,15 +1406,6 @@ function ScaleMuleProvider({
1360
1406
  setCachedUser(null);
1361
1407
  }
1362
1408
  }
1363
- } catch {
1364
- if (mounted) {
1365
- client.setCookieSession(null);
1366
- setUser(null);
1367
- setCachedUser(null);
1368
- if (debug) {
1369
- console.debug("[ScaleMule] Auth proxy session check failed; clearing cached user");
1370
- }
1371
- }
1372
1409
  } finally {
1373
1410
  client.resolveSessionPending();
1374
1411
  }
@@ -1515,26 +1552,6 @@ function useMoneyClient() {
1515
1552
  const { money } = useScaleMule();
1516
1553
  return money;
1517
1554
  }
1518
-
1519
- // src/session-errors.ts
1520
- var SESSION_ENDED_CODES = /* @__PURE__ */ new Set([
1521
- "UNAUTHORIZED",
1522
- "INVALID_SESSION",
1523
- "SESSION_EXPIRED",
1524
- "SESSION_IDLE_EXPIRED",
1525
- "SESSION_ABSOLUTE_EXPIRED",
1526
- "SESSION_REVOKED",
1527
- "TOKEN_EXPIRED",
1528
- "TOKEN_INVALID"
1529
- ]);
1530
- function isSessionEndedError(error) {
1531
- if (!error || typeof error !== "object") return false;
1532
- const { status, code } = error;
1533
- if (status === 401) return true;
1534
- return typeof code === "string" && SESSION_ENDED_CODES.has(code);
1535
- }
1536
-
1537
- // src/hooks/useAuth.ts
1538
1555
  function cookieLoginResult(user) {
1539
1556
  return { authenticated: true, userId: user.id, user };
1540
1557
  }
package/dist/index.mjs CHANGED
@@ -1129,6 +1129,24 @@ function reportSdkError(payload) {
1129
1129
  } catch {
1130
1130
  }
1131
1131
  }
1132
+
1133
+ // src/session-errors.ts
1134
+ var SESSION_ENDED_CODES = /* @__PURE__ */ new Set([
1135
+ "UNAUTHORIZED",
1136
+ "INVALID_SESSION",
1137
+ "SESSION_EXPIRED",
1138
+ "SESSION_IDLE_EXPIRED",
1139
+ "SESSION_ABSOLUTE_EXPIRED",
1140
+ "SESSION_REVOKED",
1141
+ "TOKEN_EXPIRED",
1142
+ "TOKEN_INVALID"
1143
+ ]);
1144
+ function isSessionEndedError(error) {
1145
+ if (!error || typeof error !== "object") return false;
1146
+ const { status, code } = error;
1147
+ if (status === 401) return true;
1148
+ return typeof code === "string" && SESSION_ENDED_CODES.has(code);
1149
+ }
1132
1150
  var USER_CACHE_KEY = "scalemule_user";
1133
1151
  function getCachedUser() {
1134
1152
  if (typeof window === "undefined") return null;
@@ -1150,6 +1168,26 @@ function setCachedUser(user) {
1150
1168
  } catch {
1151
1169
  }
1152
1170
  }
1171
+ var PROXY_SESSION_RETRY_DELAYS_MS = [1e3, 3e3];
1172
+ var PROXY_SESSION_CHECK_TIMEOUT_MS = 1e4;
1173
+ async function checkProxySession(authProxyUrl) {
1174
+ let response;
1175
+ let body;
1176
+ try {
1177
+ const timeout = typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function" ? AbortSignal.timeout(PROXY_SESSION_CHECK_TIMEOUT_MS) : void 0;
1178
+ response = await fetch(`${authProxyUrl}/me`, { credentials: "include", signal: timeout });
1179
+ body = await response.json().catch(() => null);
1180
+ } catch {
1181
+ return { kind: "failed" };
1182
+ }
1183
+ if (body?.success && body.data?.user) {
1184
+ return { kind: "user", data: body.data };
1185
+ }
1186
+ if (response.status === 401 || isSessionEndedError(body?.error) || isSessionEndedError(body?.data?.error) || response.ok && body?.success === true) {
1187
+ return { kind: "ended" };
1188
+ }
1189
+ return { kind: "failed" };
1190
+ }
1153
1191
  var ScaleMuleContext = createContext(null);
1154
1192
  function ScaleMuleProvider({
1155
1193
  apiKey,
@@ -1322,17 +1360,25 @@ function ScaleMuleProvider({
1322
1360
  setInitializing(false);
1323
1361
  }
1324
1362
  try {
1325
- const response = await fetch(`${authProxyUrl}/me`, {
1326
- credentials: "include"
1327
- });
1328
- const data = await response.json();
1363
+ let check = await checkProxySession(authProxyUrl);
1364
+ for (const delay of PROXY_SESSION_RETRY_DELAYS_MS) {
1365
+ if (check.kind !== "failed" || !mounted) break;
1366
+ await new Promise((resolve) => setTimeout(resolve, delay));
1367
+ check = await checkProxySession(authProxyUrl);
1368
+ }
1329
1369
  if (mounted) {
1330
- if (data.success && data.data?.user) {
1331
- client.setCookieSession(data.data.user.id);
1332
- setUser(data.data.user);
1333
- setCachedUser(data.data.user);
1334
- if (data.data.sessionToken) {
1335
- await client.setSession(data.data.sessionToken, data.data.userId || "");
1370
+ if (check.kind === "user") {
1371
+ const data = check.data;
1372
+ client.setCookieSession(data.user.id);
1373
+ setUser(data.user);
1374
+ setCachedUser(data.user);
1375
+ if (data.sessionToken) {
1376
+ await client.setSession(data.sessionToken, data.userId || "");
1377
+ }
1378
+ } else if (check.kind === "failed" && cachedUser) {
1379
+ client.setCookieSession(cachedUser.id);
1380
+ if (debug) {
1381
+ console.debug("[ScaleMule] Auth proxy session check failed; keeping the signed-in user");
1336
1382
  }
1337
1383
  } else {
1338
1384
  client.setCookieSession(null);
@@ -1340,15 +1386,6 @@ function ScaleMuleProvider({
1340
1386
  setCachedUser(null);
1341
1387
  }
1342
1388
  }
1343
- } catch {
1344
- if (mounted) {
1345
- client.setCookieSession(null);
1346
- setUser(null);
1347
- setCachedUser(null);
1348
- if (debug) {
1349
- console.debug("[ScaleMule] Auth proxy session check failed; clearing cached user");
1350
- }
1351
- }
1352
1389
  } finally {
1353
1390
  client.resolveSessionPending();
1354
1391
  }
@@ -1495,26 +1532,6 @@ function useMoneyClient() {
1495
1532
  const { money } = useScaleMule();
1496
1533
  return money;
1497
1534
  }
1498
-
1499
- // src/session-errors.ts
1500
- var SESSION_ENDED_CODES = /* @__PURE__ */ new Set([
1501
- "UNAUTHORIZED",
1502
- "INVALID_SESSION",
1503
- "SESSION_EXPIRED",
1504
- "SESSION_IDLE_EXPIRED",
1505
- "SESSION_ABSOLUTE_EXPIRED",
1506
- "SESSION_REVOKED",
1507
- "TOKEN_EXPIRED",
1508
- "TOKEN_INVALID"
1509
- ]);
1510
- function isSessionEndedError(error) {
1511
- if (!error || typeof error !== "object") return false;
1512
- const { status, code } = error;
1513
- if (status === 401) return true;
1514
- return typeof code === "string" && SESSION_ENDED_CODES.has(code);
1515
- }
1516
-
1517
- // src/hooks/useAuth.ts
1518
1535
  function cookieLoginResult(user) {
1519
1536
  return { authenticated: true, userId: user.id, user };
1520
1537
  }
@@ -1325,7 +1325,7 @@ function isSameOriginRequest(request) {
1325
1325
  }
1326
1326
  return true;
1327
1327
  }
1328
- var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "video", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1328
+ var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "photos", "video", "videos", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1329
1329
  var AUTH_ROUTES = /* @__PURE__ */ new Set([
1330
1330
  "GET me",
1331
1331
  "GET mfa/status",
@@ -1922,11 +1922,25 @@ function createAuthRoutes(config = {}) {
1922
1922
  rotated = newToken;
1923
1923
  }
1924
1924
  });
1925
- } catch {
1926
- return withNorm(clearSession(
1927
- { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
1928
- cookieOptions
1929
- ));
1925
+ } catch (err) {
1926
+ const apiErr = err instanceof ScaleMuleApiError ? err : null;
1927
+ if (isSessionEndedError(apiErr)) {
1928
+ return withNorm(clearSession(
1929
+ { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
1930
+ cookieOptions
1931
+ ));
1932
+ }
1933
+ const failure = errorResponse(
1934
+ apiErr?.code || "SESSION_CHECK_FAILED",
1935
+ "The session could not be checked",
1936
+ apiErr?.status && apiErr.status >= 400 ? apiErr.status : 503
1937
+ );
1938
+ const refreshedToken = rotated;
1939
+ if (refreshedToken) {
1940
+ const carrier = withRefreshedSession(refreshedToken, session.userId, {}, cookieOptions);
1941
+ for (const cookie of carrier.headers.getSetCookie()) failure.headers.append("Set-Cookie", cookie);
1942
+ }
1943
+ return withNorm(failure);
1930
1944
  }
1931
1945
  if (rotated) {
1932
1946
  return withNorm(withRefreshedSession(
@@ -1323,7 +1323,7 @@ function isSameOriginRequest(request) {
1323
1323
  }
1324
1324
  return true;
1325
1325
  }
1326
- var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "video", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1326
+ var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "photos", "video", "videos", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1327
1327
  var AUTH_ROUTES = /* @__PURE__ */ new Set([
1328
1328
  "GET me",
1329
1329
  "GET mfa/status",
@@ -1920,11 +1920,25 @@ function createAuthRoutes(config = {}) {
1920
1920
  rotated = newToken;
1921
1921
  }
1922
1922
  });
1923
- } catch {
1924
- return withNorm(clearSession(
1925
- { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
1926
- cookieOptions
1927
- ));
1923
+ } catch (err) {
1924
+ const apiErr = err instanceof ScaleMuleApiError ? err : null;
1925
+ if (isSessionEndedError(apiErr)) {
1926
+ return withNorm(clearSession(
1927
+ { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
1928
+ cookieOptions
1929
+ ));
1930
+ }
1931
+ const failure = errorResponse(
1932
+ apiErr?.code || "SESSION_CHECK_FAILED",
1933
+ "The session could not be checked",
1934
+ apiErr?.status && apiErr.status >= 400 ? apiErr.status : 503
1935
+ );
1936
+ const refreshedToken = rotated;
1937
+ if (refreshedToken) {
1938
+ const carrier = withRefreshedSession(refreshedToken, session.userId, {}, cookieOptions);
1939
+ for (const cookie of carrier.headers.getSetCookie()) failure.headers.append("Set-Cookie", cookie);
1940
+ }
1941
+ return withNorm(failure);
1928
1942
  }
1929
1943
  if (rotated) {
1930
1944
  return withNorm(withRefreshedSession(
@@ -1503,7 +1503,7 @@ function isSameOriginRequest(request) {
1503
1503
  }
1504
1504
  return true;
1505
1505
  }
1506
- var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "video", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1506
+ var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "photos", "video", "videos", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1507
1507
  var AUTH_ROUTES = /* @__PURE__ */ new Set([
1508
1508
  "GET me",
1509
1509
  "GET mfa/status",
@@ -2100,11 +2100,25 @@ function createAuthRoutes(config = {}) {
2100
2100
  rotated = newToken;
2101
2101
  }
2102
2102
  });
2103
- } catch {
2104
- return withNorm(clearSession(
2105
- { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
2106
- cookieOptions
2107
- ));
2103
+ } catch (err) {
2104
+ const apiErr = err instanceof ScaleMuleApiError ? err : null;
2105
+ if (isSessionEndedError(apiErr)) {
2106
+ return withNorm(clearSession(
2107
+ { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
2108
+ cookieOptions
2109
+ ));
2110
+ }
2111
+ const failure = errorResponse(
2112
+ apiErr?.code || "SESSION_CHECK_FAILED",
2113
+ "The session could not be checked",
2114
+ apiErr?.status && apiErr.status >= 400 ? apiErr.status : 503
2115
+ );
2116
+ const refreshedToken = rotated;
2117
+ if (refreshedToken) {
2118
+ const carrier = withRefreshedSession(refreshedToken, session.userId, {}, cookieOptions);
2119
+ for (const cookie of carrier.headers.getSetCookie()) failure.headers.append("Set-Cookie", cookie);
2120
+ }
2121
+ return withNorm(failure);
2108
2122
  }
2109
2123
  if (rotated) {
2110
2124
  return withNorm(withRefreshedSession(
@@ -1501,7 +1501,7 @@ function isSameOriginRequest(request) {
1501
1501
  }
1502
1502
  return true;
1503
1503
  }
1504
- var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "video", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1504
+ var SERVICES = /* @__PURE__ */ new Set(["storage", "photo", "photos", "video", "videos", "audio", "media", "tts", "social", "chat", "realtime", "money", "billing", "flags", "notifications", "search", "presence", "conference", "gallop", "data", "forms", "preferences", "feedback", "referrals"]);
1505
1505
  var AUTH_ROUTES = /* @__PURE__ */ new Set([
1506
1506
  "GET me",
1507
1507
  "GET mfa/status",
@@ -2098,11 +2098,25 @@ function createAuthRoutes(config = {}) {
2098
2098
  rotated = newToken;
2099
2099
  }
2100
2100
  });
2101
- } catch {
2102
- return withNorm(clearSession(
2103
- { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
2104
- cookieOptions
2105
- ));
2101
+ } catch (err) {
2102
+ const apiErr = err instanceof ScaleMuleApiError ? err : null;
2103
+ if (isSessionEndedError(apiErr)) {
2104
+ return withNorm(clearSession(
2105
+ { error: { code: "SESSION_EXPIRED", message: "Session expired" } },
2106
+ cookieOptions
2107
+ ));
2108
+ }
2109
+ const failure = errorResponse(
2110
+ apiErr?.code || "SESSION_CHECK_FAILED",
2111
+ "The session could not be checked",
2112
+ apiErr?.status && apiErr.status >= 400 ? apiErr.status : 503
2113
+ );
2114
+ const refreshedToken = rotated;
2115
+ if (refreshedToken) {
2116
+ const carrier = withRefreshedSession(refreshedToken, session.userId, {}, cookieOptions);
2117
+ for (const cookie of carrier.headers.getSetCookie()) failure.headers.append("Set-Cookie", cookie);
2118
+ }
2119
+ return withNorm(failure);
2106
2120
  }
2107
2121
  if (rotated) {
2108
2122
  return withNorm(withRefreshedSession(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/nextjs",
3
- "version": "0.1.74",
3
+ "version": "0.1.76",
4
4
  "description": "ScaleMule SDK for Next.js applications - authentication, storage, and user management",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -93,7 +93,7 @@
93
93
  "next": "^16.1.6",
94
94
  "react": "^19.2.4",
95
95
  "react-dom": "^19.2.4",
96
- "tsup": "^8.0.0",
96
+ "tsup": "^9.0.0",
97
97
  "typescript": "^5.0.0",
98
98
  "vitest": "^4.0.18"
99
99
  },