@scalemule/nextjs 0.1.52 → 0.1.54
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 +51 -5
- package/dist/client.d.mts +6 -1
- package/dist/client.d.ts +6 -1
- package/dist/client.js +24 -4
- package/dist/client.mjs +24 -4
- package/dist/{index-BQgmT53x.d.mts → index-2PnO4RAF.d.mts} +19 -5
- package/dist/{index-BQgmT53x.d.ts → index-2PnO4RAF.d.ts} +19 -5
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +88 -39
- package/dist/index.mjs +88 -39
- package/dist/server/auth.d.mts +7 -2
- package/dist/server/auth.d.ts +7 -2
- package/dist/server/auth.js +231 -38
- package/dist/server/auth.mjs +231 -39
- package/dist/server/index.d.mts +24 -5
- package/dist/server/index.d.ts +24 -5
- package/dist/server/index.js +256 -64
- package/dist/server/index.mjs +256 -64
- package/dist/server/webhook-handler.d.mts +2 -2
- package/dist/server/webhook-handler.d.ts +2 -2
- package/dist/testing.d.mts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/{webhook-handler-44RRJH-r.d.ts → webhook-handler-1nnrmWT0.d.ts} +11 -1
- package/dist/{webhook-handler-DKt6H8FB.d.mts → webhook-handler-DNpCXDDA.d.mts} +11 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -63,7 +63,7 @@ function useSecurityChallenge(recoveryUrl) {
|
|
|
63
63
|
event.preventDefault();
|
|
64
64
|
answer(null);
|
|
65
65
|
},
|
|
66
|
-
style: { border: "1px solid #d1d5db", borderRadius: 16, padding: 28, maxWidth: 420, width: "calc(100% - 48px)", color: "
|
|
66
|
+
style: { border: "1px solid var(--sm-security-border, var(--site-hairline, #d1d5db))", borderRadius: 16, padding: 28, maxWidth: 420, width: "calc(100% - 48px)", color: "var(--sm-security-text, var(--site-ink, CanvasText))", background: "var(--sm-security-background, var(--site-bg-elevated, Canvas))", colorScheme: "light dark", boxSizing: "border-box" },
|
|
67
67
|
children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: (event) => {
|
|
68
68
|
event.preventDefault();
|
|
69
69
|
if (code.trim()) answer(code.trim());
|
|
@@ -83,10 +83,10 @@ function useSecurityChallenge(recoveryUrl) {
|
|
|
83
83
|
onChange: (event) => setCode(event.target.value),
|
|
84
84
|
maxLength: 32,
|
|
85
85
|
required: true,
|
|
86
|
-
style: { display: "block", boxSizing: "border-box", width: "100%", margin: "8px 0 20px", padding: 12, fontSize: 22, border: "1px solid #9ca3af", borderRadius: 8 }
|
|
86
|
+
style: { display: "block", boxSizing: "border-box", width: "100%", margin: "8px 0 20px", padding: 12, fontSize: 22, border: "1px solid var(--sm-security-border, var(--site-hairline-strong, #9ca3af))", background: "inherit", color: "inherit", borderRadius: 8 }
|
|
87
87
|
}
|
|
88
88
|
),
|
|
89
|
-
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "submit", style: { padding: "10px 20px", borderRadius: 8, border: 0, background: "#2563eb", color: "#fff", cursor: "pointer" }, children: "Continue" }),
|
|
89
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "submit", style: { padding: "10px 20px", borderRadius: 8, border: 0, background: "var(--sm-security-accent, var(--site-accent, #2563eb))", color: "#fff", cursor: "pointer" }, children: "Continue" }),
|
|
90
90
|
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: () => answer(null), style: { marginLeft: 12 }, children: "Cancel" }),
|
|
91
91
|
challenge.method !== "totp" && /* @__PURE__ */ jsxRuntime.jsxs("p", { children: [
|
|
92
92
|
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: () => answer("resend"), children: "Send a new code" }),
|
|
@@ -467,6 +467,7 @@ var ScaleMuleClient = class {
|
|
|
467
467
|
// The shared helper also single-flights at the storage-adapter level.
|
|
468
468
|
this.anonymousIdPromise = null;
|
|
469
469
|
this.refreshPromise = null;
|
|
470
|
+
this.cookieSession = config.cookieSession === true;
|
|
470
471
|
this.apiKey = config.apiKey;
|
|
471
472
|
this.applicationId = config.applicationId || null;
|
|
472
473
|
this.gatewayUrl = resolveGatewayUrl(config);
|
|
@@ -596,8 +597,13 @@ var ScaleMuleClient = class {
|
|
|
596
597
|
* Initialize client by loading persisted session
|
|
597
598
|
*/
|
|
598
599
|
async initialize() {
|
|
599
|
-
|
|
600
|
-
|
|
600
|
+
if (this.cookieSession) {
|
|
601
|
+
await this.storage.removeItem(SESSION_STORAGE_KEY);
|
|
602
|
+
await this.storage.removeItem(sdk.STORAGE_KEYS.SESSION_POOL);
|
|
603
|
+
await this.storage.removeItem(sdk.STORAGE_KEYS.ACTIVE_ACCOUNT);
|
|
604
|
+
}
|
|
605
|
+
const token = this.cookieSession ? null : await this.storage.getItem(SESSION_STORAGE_KEY);
|
|
606
|
+
const userId = this.cookieSession ? null : await this.storage.getItem(USER_ID_STORAGE_KEY);
|
|
601
607
|
if (token) this.sessionToken = token;
|
|
602
608
|
if (userId) this.userId = userId;
|
|
603
609
|
const wsId = await this.storage.getItem(WORKSPACE_STORAGE_KEY);
|
|
@@ -643,6 +649,11 @@ var ScaleMuleClient = class {
|
|
|
643
649
|
* Set session after login
|
|
644
650
|
*/
|
|
645
651
|
async setSession(token, userId) {
|
|
652
|
+
if (this.cookieSession) {
|
|
653
|
+
this.setCookieSession(userId);
|
|
654
|
+
await this.storage.removeItem(SESSION_STORAGE_KEY);
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
646
657
|
this.sessionToken = token;
|
|
647
658
|
this.userId = userId;
|
|
648
659
|
await this.storage.setItem(SESSION_STORAGE_KEY, token);
|
|
@@ -663,8 +674,17 @@ var ScaleMuleClient = class {
|
|
|
663
674
|
*
|
|
664
675
|
* Pass `null` to clear without touching userId/storage.
|
|
665
676
|
*/
|
|
677
|
+
setCookieSession(userId) {
|
|
678
|
+
if (!this.cookieSession) return;
|
|
679
|
+
this.sessionToken = null;
|
|
680
|
+
this.userId = userId;
|
|
681
|
+
this.resolveSessionPending();
|
|
682
|
+
}
|
|
683
|
+
usesCookieSession() {
|
|
684
|
+
return this.cookieSession;
|
|
685
|
+
}
|
|
666
686
|
setSessionToken(token) {
|
|
667
|
-
this.sessionToken = token;
|
|
687
|
+
this.sessionToken = this.cookieSession ? null : token;
|
|
668
688
|
if (this.debug) {
|
|
669
689
|
console.log("[ScaleMule] Session token", token ? "set (token-only)" : "cleared (token-only)");
|
|
670
690
|
}
|
|
@@ -699,7 +719,7 @@ var ScaleMuleClient = class {
|
|
|
699
719
|
* Check if client has an active session
|
|
700
720
|
*/
|
|
701
721
|
isAuthenticated() {
|
|
702
|
-
return this.sessionToken !== null && this.userId !== null;
|
|
722
|
+
return (this.cookieSession || this.sessionToken !== null) && this.userId !== null;
|
|
703
723
|
}
|
|
704
724
|
/**
|
|
705
725
|
* Build headers for a request
|
|
@@ -1137,6 +1157,7 @@ function ScaleMuleProvider({
|
|
|
1137
1157
|
storage,
|
|
1138
1158
|
analyticsProxyUrl,
|
|
1139
1159
|
authProxyUrl,
|
|
1160
|
+
sessionMode,
|
|
1140
1161
|
telemetryEndpoint,
|
|
1141
1162
|
publishableKey,
|
|
1142
1163
|
enableAccountSwitcher,
|
|
@@ -1156,6 +1177,9 @@ function ScaleMuleProvider({
|
|
|
1156
1177
|
const security = useSecurityChallenge(passwordRecoveryUrl);
|
|
1157
1178
|
const requestSecurityCode = onSecurityChallenge || security.prompt;
|
|
1158
1179
|
const memberMode = typeof getToken === "function";
|
|
1180
|
+
const cookieSession = !!authProxyUrl && !memberMode && sessionMode !== "bearer";
|
|
1181
|
+
const browserKey = cookieSession ? publishableKey || process.env.NEXT_PUBLIC_SCALEMULE_PUBLISHABLE_KEY || apiKey : apiKey;
|
|
1182
|
+
const browserGateway = cookieSession ? `${authProxyUrl.replace(/\/$/, "")}/client` : void 0;
|
|
1159
1183
|
const [user, setUser] = React.useState(null);
|
|
1160
1184
|
const [initializing, setInitializing] = React.useState(true);
|
|
1161
1185
|
const [error, setError] = React.useState(null);
|
|
@@ -1166,10 +1190,11 @@ function ScaleMuleProvider({
|
|
|
1166
1190
|
const resolvedGatewayUrl = gatewayUrl || (environment === "dev" ? "https://api-dev.scalemule.com" : "https://api.scalemule.com");
|
|
1167
1191
|
const client = React.useMemo(
|
|
1168
1192
|
() => createClient({
|
|
1169
|
-
apiKey,
|
|
1193
|
+
apiKey: browserKey,
|
|
1170
1194
|
applicationId,
|
|
1171
1195
|
environment,
|
|
1172
|
-
gatewayUrl: resolvedGatewayUrl,
|
|
1196
|
+
gatewayUrl: browserGateway || resolvedGatewayUrl,
|
|
1197
|
+
cookieSession,
|
|
1173
1198
|
debug,
|
|
1174
1199
|
storage,
|
|
1175
1200
|
// Make outbound API calls wait for the first token to land in any
|
|
@@ -1178,27 +1203,28 @@ function ScaleMuleProvider({
|
|
|
1178
1203
|
// getToken() callback. Resolved in the init effect below.
|
|
1179
1204
|
pendingSessionInit: !!authProxyUrl || memberMode
|
|
1180
1205
|
}),
|
|
1181
|
-
[
|
|
1206
|
+
[browserKey, applicationId, environment, resolvedGatewayUrl, browserGateway, cookieSession, debug, storage, authProxyUrl, memberMode]
|
|
1182
1207
|
);
|
|
1183
1208
|
const money$1 = React.useMemo(
|
|
1184
1209
|
() => money.createMoneyClient({
|
|
1185
|
-
apiKey,
|
|
1186
|
-
gatewayUrl: resolvedGatewayUrl,
|
|
1210
|
+
apiKey: browserKey,
|
|
1211
|
+
gatewayUrl: browserGateway || resolvedGatewayUrl,
|
|
1187
1212
|
environment,
|
|
1188
1213
|
accessToken: client.getSessionToken() || void 0,
|
|
1189
1214
|
fetch: globalThis.fetch.bind(globalThis)
|
|
1190
1215
|
}),
|
|
1191
|
-
[
|
|
1216
|
+
[browserKey, resolvedGatewayUrl, browserGateway, environment, client]
|
|
1192
1217
|
);
|
|
1193
1218
|
const baseClient = React.useMemo(() => {
|
|
1194
1219
|
return new sdk.ScaleMule({
|
|
1195
|
-
apiKey,
|
|
1220
|
+
apiKey: browserKey,
|
|
1196
1221
|
applicationId,
|
|
1197
|
-
baseUrl: resolvedGatewayUrl,
|
|
1222
|
+
baseUrl: browserGateway || resolvedGatewayUrl,
|
|
1223
|
+
realtimeUrl: cookieSession ? resolvedGatewayUrl : void 0,
|
|
1198
1224
|
environment,
|
|
1199
1225
|
debug
|
|
1200
1226
|
});
|
|
1201
|
-
}, [
|
|
1227
|
+
}, [browserKey, applicationId, environment, resolvedGatewayUrl, browserGateway, cookieSession, debug]);
|
|
1202
1228
|
const [fetchedPolicy, setFetchedPolicy] = React.useState(void 0);
|
|
1203
1229
|
const [tokenVersion, setTokenVersion] = React.useState(0);
|
|
1204
1230
|
React.useEffect(() => {
|
|
@@ -1288,18 +1314,21 @@ function ScaleMuleProvider({
|
|
|
1288
1314
|
const data = await response.json();
|
|
1289
1315
|
if (mounted) {
|
|
1290
1316
|
if (data.success && data.data?.user) {
|
|
1317
|
+
client.setCookieSession(data.data.user.id);
|
|
1291
1318
|
setUser(data.data.user);
|
|
1292
1319
|
setCachedUser(data.data.user);
|
|
1293
1320
|
if (data.data.sessionToken) {
|
|
1294
1321
|
await client.setSession(data.data.sessionToken, data.data.userId || "");
|
|
1295
1322
|
}
|
|
1296
1323
|
} else {
|
|
1324
|
+
client.setCookieSession(null);
|
|
1297
1325
|
setUser(null);
|
|
1298
1326
|
setCachedUser(null);
|
|
1299
1327
|
}
|
|
1300
1328
|
}
|
|
1301
1329
|
} catch {
|
|
1302
1330
|
if (mounted) {
|
|
1331
|
+
client.setCookieSession(null);
|
|
1303
1332
|
setUser(null);
|
|
1304
1333
|
setCachedUser(null);
|
|
1305
1334
|
if (debug) {
|
|
@@ -1386,13 +1415,15 @@ function ScaleMuleProvider({
|
|
|
1386
1415
|
}, [memberMode, memberTokenPollMs, getToken, client, baseClient, money$1, debug]);
|
|
1387
1416
|
const handleSetUser = React.useCallback(
|
|
1388
1417
|
(newUser) => {
|
|
1418
|
+
client.setCookieSession(newUser?.id || null);
|
|
1419
|
+
if (!newUser) baseClient.realtime.disconnect();
|
|
1389
1420
|
setUser(newUser);
|
|
1390
1421
|
setCachedUser(newUser);
|
|
1391
1422
|
if (newUser === null && onLogout) {
|
|
1392
1423
|
onLogout();
|
|
1393
1424
|
}
|
|
1394
1425
|
},
|
|
1395
|
-
[onLogout]
|
|
1426
|
+
[onLogout, client, baseClient]
|
|
1396
1427
|
);
|
|
1397
1428
|
const value = React.useMemo(
|
|
1398
1429
|
() => ({
|
|
@@ -1416,7 +1447,7 @@ function ScaleMuleProvider({
|
|
|
1416
1447
|
setError,
|
|
1417
1448
|
analyticsProxyUrl,
|
|
1418
1449
|
authProxyUrl,
|
|
1419
|
-
publishableKey,
|
|
1450
|
+
publishableKey: cookieSession ? browserKey : publishableKey,
|
|
1420
1451
|
apiKey,
|
|
1421
1452
|
gatewayUrl: resolvedGatewayUrl,
|
|
1422
1453
|
environment: environment || void 0,
|
|
@@ -1424,7 +1455,7 @@ function ScaleMuleProvider({
|
|
|
1424
1455
|
accountSwitcherPrivacy,
|
|
1425
1456
|
bootstrapFlags
|
|
1426
1457
|
}),
|
|
1427
|
-
[requestSecurityCode, client, money$1, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
|
|
1458
|
+
[requestSecurityCode, client, money$1, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, browserKey, cookieSession, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
|
|
1428
1459
|
);
|
|
1429
1460
|
return /* @__PURE__ */ jsxRuntime.jsxs(ScaleMuleContext.Provider, { value, children: [
|
|
1430
1461
|
children,
|
|
@@ -1448,6 +1479,9 @@ function useMoneyClient() {
|
|
|
1448
1479
|
const { money } = useScaleMule();
|
|
1449
1480
|
return money;
|
|
1450
1481
|
}
|
|
1482
|
+
function cookieLoginResult(user) {
|
|
1483
|
+
return { authenticated: true, userId: user.id, user };
|
|
1484
|
+
}
|
|
1451
1485
|
function maskEmail(email) {
|
|
1452
1486
|
const [local, domain] = email.split("@");
|
|
1453
1487
|
if (!domain) return "***@***.***";
|
|
@@ -1503,7 +1537,7 @@ async function getProxyAnonymousId() {
|
|
|
1503
1537
|
}
|
|
1504
1538
|
async function proxyFetch(proxyUrl, path, options = {}) {
|
|
1505
1539
|
const method = options.method || "POST";
|
|
1506
|
-
const headers = {};
|
|
1540
|
+
const headers = { "Content-Type": "application/json" };
|
|
1507
1541
|
if (options.body) {
|
|
1508
1542
|
headers["Content-Type"] = "application/json";
|
|
1509
1543
|
}
|
|
@@ -1562,7 +1596,7 @@ function useAuth() {
|
|
|
1562
1596
|
if (response.data.sessionToken) {
|
|
1563
1597
|
await client.setSession(response.data.sessionToken, response.data.userId || response.data.user?.id || "");
|
|
1564
1598
|
}
|
|
1565
|
-
if (response.data.user) {
|
|
1599
|
+
if (response.data.user && response.data.authenticated !== false) {
|
|
1566
1600
|
setUser(response.data.user);
|
|
1567
1601
|
}
|
|
1568
1602
|
return response.data.user;
|
|
@@ -1612,15 +1646,16 @@ function useAuth() {
|
|
|
1612
1646
|
}
|
|
1613
1647
|
const loginData2 = response.data;
|
|
1614
1648
|
const responseUser = "user" in loginData2 ? loginData2.user : null;
|
|
1615
|
-
if (responseUser) {
|
|
1616
|
-
|
|
1649
|
+
if (!responseUser) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a user" });
|
|
1650
|
+
setUser(responseUser);
|
|
1651
|
+
if (client.usesCookieSession()) {
|
|
1652
|
+
client.setCookieSession(responseUser.id);
|
|
1653
|
+
return cookieLoginResult(responseUser);
|
|
1617
1654
|
}
|
|
1618
|
-
const sessionToken =
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
}
|
|
1623
|
-
return response.data;
|
|
1655
|
+
const sessionToken = loginData2.sessionToken || loginData2.session_token;
|
|
1656
|
+
if (!sessionToken) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a session" });
|
|
1657
|
+
await client.setSession(sessionToken, loginData2.userId || responseUser.id);
|
|
1658
|
+
return { ...loginData2, session_token: sessionToken };
|
|
1624
1659
|
}
|
|
1625
1660
|
let loginResult;
|
|
1626
1661
|
try {
|
|
@@ -1651,6 +1686,7 @@ function useAuth() {
|
|
|
1651
1686
|
await proxyFetch(authProxyUrl, "logout");
|
|
1652
1687
|
} catch {
|
|
1653
1688
|
}
|
|
1689
|
+
await client.clearSession();
|
|
1654
1690
|
setUser(null);
|
|
1655
1691
|
return;
|
|
1656
1692
|
}
|
|
@@ -1825,6 +1861,9 @@ function useAuth() {
|
|
|
1825
1861
|
setError(err);
|
|
1826
1862
|
throw err;
|
|
1827
1863
|
}
|
|
1864
|
+
if (!client.usesCookieSession() && response.data?.sessionToken && response.data.userId) {
|
|
1865
|
+
await client.setSession(response.data.sessionToken, response.data.userId);
|
|
1866
|
+
}
|
|
1828
1867
|
if (response.data?.user) {
|
|
1829
1868
|
setUser(response.data.user);
|
|
1830
1869
|
}
|
|
@@ -1905,7 +1944,8 @@ function useAuth() {
|
|
|
1905
1944
|
}
|
|
1906
1945
|
throw err;
|
|
1907
1946
|
}
|
|
1908
|
-
|
|
1947
|
+
if (client.usesCookieSession()) client.setCookieSession(callbackData.user.id);
|
|
1948
|
+
else if ("session_token" in callbackData) await client.setSession(callbackData.session_token, callbackData.user.id);
|
|
1909
1949
|
setUser(callbackData.user);
|
|
1910
1950
|
return callbackData;
|
|
1911
1951
|
},
|
|
@@ -2008,8 +2048,15 @@ function useAuth() {
|
|
|
2008
2048
|
const result = await proxyFetch(authProxyUrl, "mfa/verify", { body: { pending_token: challengeToken, code, method } });
|
|
2009
2049
|
if (!result.success || !result.data) throw result.error;
|
|
2010
2050
|
const sessionToken = result.data.sessionToken || result.data.session_token;
|
|
2051
|
+
if (client.usesCookieSession() && result.data.user) {
|
|
2052
|
+
client.setCookieSession(result.data.user.id);
|
|
2053
|
+
setUser(result.data.user);
|
|
2054
|
+
return cookieLoginResult(result.data.user);
|
|
2055
|
+
}
|
|
2011
2056
|
if (!sessionToken) throw new ScaleMuleApiError({ code: "MFA_FAILED", message: "Sign-in did not return a session" });
|
|
2012
|
-
|
|
2057
|
+
await client.setSession(sessionToken, result.data.user.id);
|
|
2058
|
+
setUser(result.data.user);
|
|
2059
|
+
return { ...result.data, session_token: sessionToken };
|
|
2013
2060
|
} else {
|
|
2014
2061
|
mfaResult = await client.post("/v1/auth/mfa/verify", { pending_token: challengeToken, code, method });
|
|
2015
2062
|
}
|
|
@@ -2141,15 +2188,16 @@ function useAuth() {
|
|
|
2141
2188
|
}
|
|
2142
2189
|
const loginData = response.data;
|
|
2143
2190
|
const responseUser = "user" in loginData ? loginData.user : null;
|
|
2144
|
-
if (responseUser) {
|
|
2145
|
-
|
|
2191
|
+
if (!responseUser) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a user" });
|
|
2192
|
+
setUser(responseUser);
|
|
2193
|
+
if (client.usesCookieSession()) {
|
|
2194
|
+
client.setCookieSession(responseUser.id);
|
|
2195
|
+
return cookieLoginResult(responseUser);
|
|
2146
2196
|
}
|
|
2147
|
-
const sessionToken =
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
}
|
|
2152
|
-
return response.data;
|
|
2197
|
+
const sessionToken = loginData.sessionToken || loginData.session_token;
|
|
2198
|
+
if (!sessionToken) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a session" });
|
|
2199
|
+
await client.setSession(sessionToken, loginData.userId || responseUser.id);
|
|
2200
|
+
return { ...loginData, session_token: sessionToken };
|
|
2153
2201
|
}
|
|
2154
2202
|
let phoneLoginData;
|
|
2155
2203
|
try {
|
|
@@ -2193,6 +2241,7 @@ function useAuth() {
|
|
|
2193
2241
|
if (!target) return null;
|
|
2194
2242
|
if (authProxyUrl) {
|
|
2195
2243
|
await proxyFetch(authProxyUrl, "switch-account");
|
|
2244
|
+
await client.clearSession();
|
|
2196
2245
|
} else {
|
|
2197
2246
|
const sessionToken = client.getSessionToken();
|
|
2198
2247
|
if (sessionToken) {
|
package/dist/index.mjs
CHANGED
|
@@ -43,7 +43,7 @@ function useSecurityChallenge(recoveryUrl) {
|
|
|
43
43
|
event.preventDefault();
|
|
44
44
|
answer(null);
|
|
45
45
|
},
|
|
46
|
-
style: { border: "1px solid #d1d5db", borderRadius: 16, padding: 28, maxWidth: 420, width: "calc(100% - 48px)", color: "
|
|
46
|
+
style: { border: "1px solid var(--sm-security-border, var(--site-hairline, #d1d5db))", borderRadius: 16, padding: 28, maxWidth: 420, width: "calc(100% - 48px)", color: "var(--sm-security-text, var(--site-ink, CanvasText))", background: "var(--sm-security-background, var(--site-bg-elevated, Canvas))", colorScheme: "light dark", boxSizing: "border-box" },
|
|
47
47
|
children: /* @__PURE__ */ jsxs("form", { onSubmit: (event) => {
|
|
48
48
|
event.preventDefault();
|
|
49
49
|
if (code.trim()) answer(code.trim());
|
|
@@ -63,10 +63,10 @@ function useSecurityChallenge(recoveryUrl) {
|
|
|
63
63
|
onChange: (event) => setCode(event.target.value),
|
|
64
64
|
maxLength: 32,
|
|
65
65
|
required: true,
|
|
66
|
-
style: { display: "block", boxSizing: "border-box", width: "100%", margin: "8px 0 20px", padding: 12, fontSize: 22, border: "1px solid #9ca3af", borderRadius: 8 }
|
|
66
|
+
style: { display: "block", boxSizing: "border-box", width: "100%", margin: "8px 0 20px", padding: 12, fontSize: 22, border: "1px solid var(--sm-security-border, var(--site-hairline-strong, #9ca3af))", background: "inherit", color: "inherit", borderRadius: 8 }
|
|
67
67
|
}
|
|
68
68
|
),
|
|
69
|
-
/* @__PURE__ */ jsx("button", { type: "submit", style: { padding: "10px 20px", borderRadius: 8, border: 0, background: "#2563eb", color: "#fff", cursor: "pointer" }, children: "Continue" }),
|
|
69
|
+
/* @__PURE__ */ jsx("button", { type: "submit", style: { padding: "10px 20px", borderRadius: 8, border: 0, background: "var(--sm-security-accent, var(--site-accent, #2563eb))", color: "#fff", cursor: "pointer" }, children: "Continue" }),
|
|
70
70
|
/* @__PURE__ */ jsx("button", { type: "button", onClick: () => answer(null), style: { marginLeft: 12 }, children: "Cancel" }),
|
|
71
71
|
challenge.method !== "totp" && /* @__PURE__ */ jsxs("p", { children: [
|
|
72
72
|
/* @__PURE__ */ jsx("button", { type: "button", onClick: () => answer("resend"), children: "Send a new code" }),
|
|
@@ -447,6 +447,7 @@ var ScaleMuleClient = class {
|
|
|
447
447
|
// The shared helper also single-flights at the storage-adapter level.
|
|
448
448
|
this.anonymousIdPromise = null;
|
|
449
449
|
this.refreshPromise = null;
|
|
450
|
+
this.cookieSession = config.cookieSession === true;
|
|
450
451
|
this.apiKey = config.apiKey;
|
|
451
452
|
this.applicationId = config.applicationId || null;
|
|
452
453
|
this.gatewayUrl = resolveGatewayUrl(config);
|
|
@@ -576,8 +577,13 @@ var ScaleMuleClient = class {
|
|
|
576
577
|
* Initialize client by loading persisted session
|
|
577
578
|
*/
|
|
578
579
|
async initialize() {
|
|
579
|
-
|
|
580
|
-
|
|
580
|
+
if (this.cookieSession) {
|
|
581
|
+
await this.storage.removeItem(SESSION_STORAGE_KEY);
|
|
582
|
+
await this.storage.removeItem(STORAGE_KEYS.SESSION_POOL);
|
|
583
|
+
await this.storage.removeItem(STORAGE_KEYS.ACTIVE_ACCOUNT);
|
|
584
|
+
}
|
|
585
|
+
const token = this.cookieSession ? null : await this.storage.getItem(SESSION_STORAGE_KEY);
|
|
586
|
+
const userId = this.cookieSession ? null : await this.storage.getItem(USER_ID_STORAGE_KEY);
|
|
581
587
|
if (token) this.sessionToken = token;
|
|
582
588
|
if (userId) this.userId = userId;
|
|
583
589
|
const wsId = await this.storage.getItem(WORKSPACE_STORAGE_KEY);
|
|
@@ -623,6 +629,11 @@ var ScaleMuleClient = class {
|
|
|
623
629
|
* Set session after login
|
|
624
630
|
*/
|
|
625
631
|
async setSession(token, userId) {
|
|
632
|
+
if (this.cookieSession) {
|
|
633
|
+
this.setCookieSession(userId);
|
|
634
|
+
await this.storage.removeItem(SESSION_STORAGE_KEY);
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
626
637
|
this.sessionToken = token;
|
|
627
638
|
this.userId = userId;
|
|
628
639
|
await this.storage.setItem(SESSION_STORAGE_KEY, token);
|
|
@@ -643,8 +654,17 @@ var ScaleMuleClient = class {
|
|
|
643
654
|
*
|
|
644
655
|
* Pass `null` to clear without touching userId/storage.
|
|
645
656
|
*/
|
|
657
|
+
setCookieSession(userId) {
|
|
658
|
+
if (!this.cookieSession) return;
|
|
659
|
+
this.sessionToken = null;
|
|
660
|
+
this.userId = userId;
|
|
661
|
+
this.resolveSessionPending();
|
|
662
|
+
}
|
|
663
|
+
usesCookieSession() {
|
|
664
|
+
return this.cookieSession;
|
|
665
|
+
}
|
|
646
666
|
setSessionToken(token) {
|
|
647
|
-
this.sessionToken = token;
|
|
667
|
+
this.sessionToken = this.cookieSession ? null : token;
|
|
648
668
|
if (this.debug) {
|
|
649
669
|
console.log("[ScaleMule] Session token", token ? "set (token-only)" : "cleared (token-only)");
|
|
650
670
|
}
|
|
@@ -679,7 +699,7 @@ var ScaleMuleClient = class {
|
|
|
679
699
|
* Check if client has an active session
|
|
680
700
|
*/
|
|
681
701
|
isAuthenticated() {
|
|
682
|
-
return this.sessionToken !== null && this.userId !== null;
|
|
702
|
+
return (this.cookieSession || this.sessionToken !== null) && this.userId !== null;
|
|
683
703
|
}
|
|
684
704
|
/**
|
|
685
705
|
* Build headers for a request
|
|
@@ -1117,6 +1137,7 @@ function ScaleMuleProvider({
|
|
|
1117
1137
|
storage,
|
|
1118
1138
|
analyticsProxyUrl,
|
|
1119
1139
|
authProxyUrl,
|
|
1140
|
+
sessionMode,
|
|
1120
1141
|
telemetryEndpoint,
|
|
1121
1142
|
publishableKey,
|
|
1122
1143
|
enableAccountSwitcher,
|
|
@@ -1136,6 +1157,9 @@ function ScaleMuleProvider({
|
|
|
1136
1157
|
const security = useSecurityChallenge(passwordRecoveryUrl);
|
|
1137
1158
|
const requestSecurityCode = onSecurityChallenge || security.prompt;
|
|
1138
1159
|
const memberMode = typeof getToken === "function";
|
|
1160
|
+
const cookieSession = !!authProxyUrl && !memberMode && sessionMode !== "bearer";
|
|
1161
|
+
const browserKey = cookieSession ? publishableKey || process.env.NEXT_PUBLIC_SCALEMULE_PUBLISHABLE_KEY || apiKey : apiKey;
|
|
1162
|
+
const browserGateway = cookieSession ? `${authProxyUrl.replace(/\/$/, "")}/client` : void 0;
|
|
1139
1163
|
const [user, setUser] = useState(null);
|
|
1140
1164
|
const [initializing, setInitializing] = useState(true);
|
|
1141
1165
|
const [error, setError] = useState(null);
|
|
@@ -1146,10 +1170,11 @@ function ScaleMuleProvider({
|
|
|
1146
1170
|
const resolvedGatewayUrl = gatewayUrl || (environment === "dev" ? "https://api-dev.scalemule.com" : "https://api.scalemule.com");
|
|
1147
1171
|
const client = useMemo(
|
|
1148
1172
|
() => createClient({
|
|
1149
|
-
apiKey,
|
|
1173
|
+
apiKey: browserKey,
|
|
1150
1174
|
applicationId,
|
|
1151
1175
|
environment,
|
|
1152
|
-
gatewayUrl: resolvedGatewayUrl,
|
|
1176
|
+
gatewayUrl: browserGateway || resolvedGatewayUrl,
|
|
1177
|
+
cookieSession,
|
|
1153
1178
|
debug,
|
|
1154
1179
|
storage,
|
|
1155
1180
|
// Make outbound API calls wait for the first token to land in any
|
|
@@ -1158,27 +1183,28 @@ function ScaleMuleProvider({
|
|
|
1158
1183
|
// getToken() callback. Resolved in the init effect below.
|
|
1159
1184
|
pendingSessionInit: !!authProxyUrl || memberMode
|
|
1160
1185
|
}),
|
|
1161
|
-
[
|
|
1186
|
+
[browserKey, applicationId, environment, resolvedGatewayUrl, browserGateway, cookieSession, debug, storage, authProxyUrl, memberMode]
|
|
1162
1187
|
);
|
|
1163
1188
|
const money = useMemo(
|
|
1164
1189
|
() => createMoneyClient({
|
|
1165
|
-
apiKey,
|
|
1166
|
-
gatewayUrl: resolvedGatewayUrl,
|
|
1190
|
+
apiKey: browserKey,
|
|
1191
|
+
gatewayUrl: browserGateway || resolvedGatewayUrl,
|
|
1167
1192
|
environment,
|
|
1168
1193
|
accessToken: client.getSessionToken() || void 0,
|
|
1169
1194
|
fetch: globalThis.fetch.bind(globalThis)
|
|
1170
1195
|
}),
|
|
1171
|
-
[
|
|
1196
|
+
[browserKey, resolvedGatewayUrl, browserGateway, environment, client]
|
|
1172
1197
|
);
|
|
1173
1198
|
const baseClient = useMemo(() => {
|
|
1174
1199
|
return new ScaleMule({
|
|
1175
|
-
apiKey,
|
|
1200
|
+
apiKey: browserKey,
|
|
1176
1201
|
applicationId,
|
|
1177
|
-
baseUrl: resolvedGatewayUrl,
|
|
1202
|
+
baseUrl: browserGateway || resolvedGatewayUrl,
|
|
1203
|
+
realtimeUrl: cookieSession ? resolvedGatewayUrl : void 0,
|
|
1178
1204
|
environment,
|
|
1179
1205
|
debug
|
|
1180
1206
|
});
|
|
1181
|
-
}, [
|
|
1207
|
+
}, [browserKey, applicationId, environment, resolvedGatewayUrl, browserGateway, cookieSession, debug]);
|
|
1182
1208
|
const [fetchedPolicy, setFetchedPolicy] = useState(void 0);
|
|
1183
1209
|
const [tokenVersion, setTokenVersion] = useState(0);
|
|
1184
1210
|
useEffect(() => {
|
|
@@ -1268,18 +1294,21 @@ function ScaleMuleProvider({
|
|
|
1268
1294
|
const data = await response.json();
|
|
1269
1295
|
if (mounted) {
|
|
1270
1296
|
if (data.success && data.data?.user) {
|
|
1297
|
+
client.setCookieSession(data.data.user.id);
|
|
1271
1298
|
setUser(data.data.user);
|
|
1272
1299
|
setCachedUser(data.data.user);
|
|
1273
1300
|
if (data.data.sessionToken) {
|
|
1274
1301
|
await client.setSession(data.data.sessionToken, data.data.userId || "");
|
|
1275
1302
|
}
|
|
1276
1303
|
} else {
|
|
1304
|
+
client.setCookieSession(null);
|
|
1277
1305
|
setUser(null);
|
|
1278
1306
|
setCachedUser(null);
|
|
1279
1307
|
}
|
|
1280
1308
|
}
|
|
1281
1309
|
} catch {
|
|
1282
1310
|
if (mounted) {
|
|
1311
|
+
client.setCookieSession(null);
|
|
1283
1312
|
setUser(null);
|
|
1284
1313
|
setCachedUser(null);
|
|
1285
1314
|
if (debug) {
|
|
@@ -1366,13 +1395,15 @@ function ScaleMuleProvider({
|
|
|
1366
1395
|
}, [memberMode, memberTokenPollMs, getToken, client, baseClient, money, debug]);
|
|
1367
1396
|
const handleSetUser = useCallback(
|
|
1368
1397
|
(newUser) => {
|
|
1398
|
+
client.setCookieSession(newUser?.id || null);
|
|
1399
|
+
if (!newUser) baseClient.realtime.disconnect();
|
|
1369
1400
|
setUser(newUser);
|
|
1370
1401
|
setCachedUser(newUser);
|
|
1371
1402
|
if (newUser === null && onLogout) {
|
|
1372
1403
|
onLogout();
|
|
1373
1404
|
}
|
|
1374
1405
|
},
|
|
1375
|
-
[onLogout]
|
|
1406
|
+
[onLogout, client, baseClient]
|
|
1376
1407
|
);
|
|
1377
1408
|
const value = useMemo(
|
|
1378
1409
|
() => ({
|
|
@@ -1396,7 +1427,7 @@ function ScaleMuleProvider({
|
|
|
1396
1427
|
setError,
|
|
1397
1428
|
analyticsProxyUrl,
|
|
1398
1429
|
authProxyUrl,
|
|
1399
|
-
publishableKey,
|
|
1430
|
+
publishableKey: cookieSession ? browserKey : publishableKey,
|
|
1400
1431
|
apiKey,
|
|
1401
1432
|
gatewayUrl: resolvedGatewayUrl,
|
|
1402
1433
|
environment: environment || void 0,
|
|
@@ -1404,7 +1435,7 @@ function ScaleMuleProvider({
|
|
|
1404
1435
|
accountSwitcherPrivacy,
|
|
1405
1436
|
bootstrapFlags
|
|
1406
1437
|
}),
|
|
1407
|
-
[requestSecurityCode, client, money, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
|
|
1438
|
+
[requestSecurityCode, client, money, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, browserKey, cookieSession, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
|
|
1408
1439
|
);
|
|
1409
1440
|
return /* @__PURE__ */ jsxs(ScaleMuleContext.Provider, { value, children: [
|
|
1410
1441
|
children,
|
|
@@ -1428,6 +1459,9 @@ function useMoneyClient() {
|
|
|
1428
1459
|
const { money } = useScaleMule();
|
|
1429
1460
|
return money;
|
|
1430
1461
|
}
|
|
1462
|
+
function cookieLoginResult(user) {
|
|
1463
|
+
return { authenticated: true, userId: user.id, user };
|
|
1464
|
+
}
|
|
1431
1465
|
function maskEmail(email) {
|
|
1432
1466
|
const [local, domain] = email.split("@");
|
|
1433
1467
|
if (!domain) return "***@***.***";
|
|
@@ -1483,7 +1517,7 @@ async function getProxyAnonymousId() {
|
|
|
1483
1517
|
}
|
|
1484
1518
|
async function proxyFetch(proxyUrl, path, options = {}) {
|
|
1485
1519
|
const method = options.method || "POST";
|
|
1486
|
-
const headers = {};
|
|
1520
|
+
const headers = { "Content-Type": "application/json" };
|
|
1487
1521
|
if (options.body) {
|
|
1488
1522
|
headers["Content-Type"] = "application/json";
|
|
1489
1523
|
}
|
|
@@ -1542,7 +1576,7 @@ function useAuth() {
|
|
|
1542
1576
|
if (response.data.sessionToken) {
|
|
1543
1577
|
await client.setSession(response.data.sessionToken, response.data.userId || response.data.user?.id || "");
|
|
1544
1578
|
}
|
|
1545
|
-
if (response.data.user) {
|
|
1579
|
+
if (response.data.user && response.data.authenticated !== false) {
|
|
1546
1580
|
setUser(response.data.user);
|
|
1547
1581
|
}
|
|
1548
1582
|
return response.data.user;
|
|
@@ -1592,15 +1626,16 @@ function useAuth() {
|
|
|
1592
1626
|
}
|
|
1593
1627
|
const loginData2 = response.data;
|
|
1594
1628
|
const responseUser = "user" in loginData2 ? loginData2.user : null;
|
|
1595
|
-
if (responseUser) {
|
|
1596
|
-
|
|
1629
|
+
if (!responseUser) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a user" });
|
|
1630
|
+
setUser(responseUser);
|
|
1631
|
+
if (client.usesCookieSession()) {
|
|
1632
|
+
client.setCookieSession(responseUser.id);
|
|
1633
|
+
return cookieLoginResult(responseUser);
|
|
1597
1634
|
}
|
|
1598
|
-
const sessionToken =
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
}
|
|
1603
|
-
return response.data;
|
|
1635
|
+
const sessionToken = loginData2.sessionToken || loginData2.session_token;
|
|
1636
|
+
if (!sessionToken) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a session" });
|
|
1637
|
+
await client.setSession(sessionToken, loginData2.userId || responseUser.id);
|
|
1638
|
+
return { ...loginData2, session_token: sessionToken };
|
|
1604
1639
|
}
|
|
1605
1640
|
let loginResult;
|
|
1606
1641
|
try {
|
|
@@ -1631,6 +1666,7 @@ function useAuth() {
|
|
|
1631
1666
|
await proxyFetch(authProxyUrl, "logout");
|
|
1632
1667
|
} catch {
|
|
1633
1668
|
}
|
|
1669
|
+
await client.clearSession();
|
|
1634
1670
|
setUser(null);
|
|
1635
1671
|
return;
|
|
1636
1672
|
}
|
|
@@ -1805,6 +1841,9 @@ function useAuth() {
|
|
|
1805
1841
|
setError(err);
|
|
1806
1842
|
throw err;
|
|
1807
1843
|
}
|
|
1844
|
+
if (!client.usesCookieSession() && response.data?.sessionToken && response.data.userId) {
|
|
1845
|
+
await client.setSession(response.data.sessionToken, response.data.userId);
|
|
1846
|
+
}
|
|
1808
1847
|
if (response.data?.user) {
|
|
1809
1848
|
setUser(response.data.user);
|
|
1810
1849
|
}
|
|
@@ -1885,7 +1924,8 @@ function useAuth() {
|
|
|
1885
1924
|
}
|
|
1886
1925
|
throw err;
|
|
1887
1926
|
}
|
|
1888
|
-
|
|
1927
|
+
if (client.usesCookieSession()) client.setCookieSession(callbackData.user.id);
|
|
1928
|
+
else if ("session_token" in callbackData) await client.setSession(callbackData.session_token, callbackData.user.id);
|
|
1889
1929
|
setUser(callbackData.user);
|
|
1890
1930
|
return callbackData;
|
|
1891
1931
|
},
|
|
@@ -1988,8 +2028,15 @@ function useAuth() {
|
|
|
1988
2028
|
const result = await proxyFetch(authProxyUrl, "mfa/verify", { body: { pending_token: challengeToken, code, method } });
|
|
1989
2029
|
if (!result.success || !result.data) throw result.error;
|
|
1990
2030
|
const sessionToken = result.data.sessionToken || result.data.session_token;
|
|
2031
|
+
if (client.usesCookieSession() && result.data.user) {
|
|
2032
|
+
client.setCookieSession(result.data.user.id);
|
|
2033
|
+
setUser(result.data.user);
|
|
2034
|
+
return cookieLoginResult(result.data.user);
|
|
2035
|
+
}
|
|
1991
2036
|
if (!sessionToken) throw new ScaleMuleApiError({ code: "MFA_FAILED", message: "Sign-in did not return a session" });
|
|
1992
|
-
|
|
2037
|
+
await client.setSession(sessionToken, result.data.user.id);
|
|
2038
|
+
setUser(result.data.user);
|
|
2039
|
+
return { ...result.data, session_token: sessionToken };
|
|
1993
2040
|
} else {
|
|
1994
2041
|
mfaResult = await client.post("/v1/auth/mfa/verify", { pending_token: challengeToken, code, method });
|
|
1995
2042
|
}
|
|
@@ -2121,15 +2168,16 @@ function useAuth() {
|
|
|
2121
2168
|
}
|
|
2122
2169
|
const loginData = response.data;
|
|
2123
2170
|
const responseUser = "user" in loginData ? loginData.user : null;
|
|
2124
|
-
if (responseUser) {
|
|
2125
|
-
|
|
2171
|
+
if (!responseUser) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a user" });
|
|
2172
|
+
setUser(responseUser);
|
|
2173
|
+
if (client.usesCookieSession()) {
|
|
2174
|
+
client.setCookieSession(responseUser.id);
|
|
2175
|
+
return cookieLoginResult(responseUser);
|
|
2126
2176
|
}
|
|
2127
|
-
const sessionToken =
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
}
|
|
2132
|
-
return response.data;
|
|
2177
|
+
const sessionToken = loginData.sessionToken || loginData.session_token;
|
|
2178
|
+
if (!sessionToken) throw new ScaleMuleApiError({ code: "LOGIN_FAILED", message: "Sign-in did not return a session" });
|
|
2179
|
+
await client.setSession(sessionToken, loginData.userId || responseUser.id);
|
|
2180
|
+
return { ...loginData, session_token: sessionToken };
|
|
2133
2181
|
}
|
|
2134
2182
|
let phoneLoginData;
|
|
2135
2183
|
try {
|
|
@@ -2173,6 +2221,7 @@ function useAuth() {
|
|
|
2173
2221
|
if (!target) return null;
|
|
2174
2222
|
if (authProxyUrl) {
|
|
2175
2223
|
await proxyFetch(authProxyUrl, "switch-account");
|
|
2224
|
+
await client.clearSession();
|
|
2176
2225
|
} else {
|
|
2177
2226
|
const sessionToken = client.getSessionToken();
|
|
2178
2227
|
if (sessionToken) {
|