@scalemule/nextjs 0.1.50 → 0.1.52
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 +29 -0
- package/dist/client.d.mts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.js +2 -2
- package/dist/client.mjs +2 -2
- package/dist/{index-Bv1m_qQ8.d.mts → index-BQgmT53x.d.mts} +2 -0
- package/dist/{index-Bv1m_qQ8.d.ts → index-BQgmT53x.d.ts} +2 -0
- package/dist/index.d.mts +15 -4
- package/dist/index.d.ts +15 -4
- package/dist/index.js +232 -36
- package/dist/index.mjs +232 -36
- package/dist/server/auth.js +72 -8
- package/dist/server/auth.mjs +72 -8
- package/dist/server/index.d.mts +3 -3
- package/dist/server/index.d.ts +3 -3
- package/dist/server/index.js +75 -10
- package/dist/server/index.mjs +75 -10
- 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-5ymVecTD.d.ts → webhook-handler-44RRJH-r.d.ts} +9 -1
- package/dist/{webhook-handler-CVmBvPFN.d.mts → webhook-handler-DKt6H8FB.d.mts} +9 -1
- package/docs/network-audio.md +10 -5
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,141 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { createContext, useState, useEffect, useMemo, useCallback, useContext, useRef } from 'react';
|
|
3
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
3
4
|
import { createMoneyClient } from '@scalemule/money';
|
|
4
5
|
export { MoneyClient, createMoneyClient } from '@scalemule/money';
|
|
5
6
|
import { STORAGE_KEYS, ensureAnonymousId, ScaleMule, WebPushManager } from '@scalemule/sdk';
|
|
6
|
-
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
7
7
|
|
|
8
|
-
// src/
|
|
8
|
+
// src/security-challenge.tsx
|
|
9
|
+
function useSecurityChallenge(recoveryUrl) {
|
|
10
|
+
const [challenge, setChallenge] = useState(null);
|
|
11
|
+
const [code, setCode] = useState("");
|
|
12
|
+
const resolve = useRef(null);
|
|
13
|
+
const dialog = useRef(null);
|
|
14
|
+
const answer = useCallback((value) => {
|
|
15
|
+
const pending = resolve.current;
|
|
16
|
+
resolve.current = null;
|
|
17
|
+
setChallenge(null);
|
|
18
|
+
setCode("");
|
|
19
|
+
pending?.(value);
|
|
20
|
+
}, []);
|
|
21
|
+
const prompt = useCallback((next) => {
|
|
22
|
+
resolve.current?.(null);
|
|
23
|
+
return new Promise((done) => {
|
|
24
|
+
resolve.current = done;
|
|
25
|
+
setCode("");
|
|
26
|
+
setChallenge(next);
|
|
27
|
+
});
|
|
28
|
+
}, []);
|
|
29
|
+
useEffect(() => () => {
|
|
30
|
+
resolve.current?.(null);
|
|
31
|
+
resolve.current = null;
|
|
32
|
+
}, []);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (challenge && dialog.current && !dialog.current.open) dialog.current.showModal();
|
|
35
|
+
}, [challenge]);
|
|
36
|
+
const element = challenge ? /* @__PURE__ */ jsx(
|
|
37
|
+
"dialog",
|
|
38
|
+
{
|
|
39
|
+
ref: dialog,
|
|
40
|
+
"aria-labelledby": "sm-security-title",
|
|
41
|
+
"aria-describedby": "sm-security-description",
|
|
42
|
+
onCancel: (event) => {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
answer(null);
|
|
45
|
+
},
|
|
46
|
+
style: { border: "1px solid #d1d5db", borderRadius: 16, padding: 28, maxWidth: 420, width: "calc(100% - 48px)", color: "#111827", background: "#fff", boxSizing: "border-box" },
|
|
47
|
+
children: /* @__PURE__ */ jsxs("form", { onSubmit: (event) => {
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
if (code.trim()) answer(code.trim());
|
|
50
|
+
}, children: [
|
|
51
|
+
/* @__PURE__ */ jsx("h2", { id: "sm-security-title", style: { marginTop: 0 }, children: "Confirm your sign-in" }),
|
|
52
|
+
/* @__PURE__ */ jsx("p", { id: "sm-security-description", children: challenge.method === "totp" ? "Enter a code from your authenticator app." : "Enter the verification code sent to your " + (challenge.method === "sms" ? "phone." : "email.") }),
|
|
53
|
+
challenge.error && /* @__PURE__ */ jsx("p", { role: "alert", style: { color: "#b91c1c" }, children: challenge.error }),
|
|
54
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "sm-security-code", children: "Verification code" }),
|
|
55
|
+
/* @__PURE__ */ jsx(
|
|
56
|
+
"input",
|
|
57
|
+
{
|
|
58
|
+
id: "sm-security-code",
|
|
59
|
+
autoFocus: true,
|
|
60
|
+
autoComplete: "one-time-code",
|
|
61
|
+
inputMode: "numeric",
|
|
62
|
+
value: code,
|
|
63
|
+
onChange: (event) => setCode(event.target.value),
|
|
64
|
+
maxLength: 32,
|
|
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 }
|
|
67
|
+
}
|
|
68
|
+
),
|
|
69
|
+
/* @__PURE__ */ jsx("button", { type: "submit", style: { padding: "10px 20px", borderRadius: 8, border: 0, background: "#2563eb", color: "#fff", cursor: "pointer" }, children: "Continue" }),
|
|
70
|
+
/* @__PURE__ */ jsx("button", { type: "button", onClick: () => answer(null), style: { marginLeft: 12 }, children: "Cancel" }),
|
|
71
|
+
challenge.method !== "totp" && /* @__PURE__ */ jsxs("p", { children: [
|
|
72
|
+
/* @__PURE__ */ jsx("button", { type: "button", onClick: () => answer("resend"), children: "Send a new code" }),
|
|
73
|
+
" ",
|
|
74
|
+
/* @__PURE__ */ jsx("small", { children: "(wait 60 seconds between requests)" })
|
|
75
|
+
] }),
|
|
76
|
+
/* @__PURE__ */ jsx("p", { children: /* @__PURE__ */ jsx("a", { href: recoveryUrl, onClick: () => answer(null), children: "Reset your password" }) })
|
|
77
|
+
] })
|
|
78
|
+
}
|
|
79
|
+
) : null;
|
|
80
|
+
return { prompt, element };
|
|
81
|
+
}
|
|
82
|
+
async function withAdaptiveChallenge(attempt, prompt, mfa) {
|
|
83
|
+
let proof = {};
|
|
84
|
+
let challengeToken;
|
|
85
|
+
let message;
|
|
86
|
+
for (; ; ) {
|
|
87
|
+
try {
|
|
88
|
+
return await attempt(proof);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const err = error;
|
|
91
|
+
if (err.code === "MFA_REQUIRED" && mfa) {
|
|
92
|
+
const details = JSON.parse(err.message || "{}");
|
|
93
|
+
const token = details.pending_token;
|
|
94
|
+
const method = details.mfa_method;
|
|
95
|
+
if (!token || !["totp", "email", "sms"].includes(method || "")) throw error;
|
|
96
|
+
let mfaError;
|
|
97
|
+
if (method !== "totp") {
|
|
98
|
+
try {
|
|
99
|
+
await mfa.send(token, method);
|
|
100
|
+
} catch (failure) {
|
|
101
|
+
const failed = failure;
|
|
102
|
+
if (failed.code !== "CHALLENGE_RATE_LIMITED") throw failure;
|
|
103
|
+
mfaError = failed.message;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (; ; ) {
|
|
107
|
+
const code2 = await prompt({ method, error: mfaError });
|
|
108
|
+
if (code2 === null) throw { code: "LOGIN_CANCELLED", message: "Sign-in was cancelled." };
|
|
109
|
+
try {
|
|
110
|
+
if (code2 === "resend") {
|
|
111
|
+
await mfa.send(token, method);
|
|
112
|
+
mfaError = void 0;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
return await mfa.verify(token, code2, method);
|
|
116
|
+
} catch (failure) {
|
|
117
|
+
const failed = failure;
|
|
118
|
+
if (!["INVALID_MFA_CODE", "CHALLENGE_RATE_LIMITED"].includes(failed.code || "")) throw failure;
|
|
119
|
+
mfaError = failed.message;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (err.code === "LOGIN_CHALLENGE_REQUIRED") {
|
|
124
|
+
const details = JSON.parse(err.message || "{}");
|
|
125
|
+
if (!details.challenge_token) throw error;
|
|
126
|
+
challengeToken = details.challenge_token;
|
|
127
|
+
message = void 0;
|
|
128
|
+
} else if (challengeToken && ["LOGIN_CHALLENGE_INVALID", "CHALLENGE_RATE_LIMITED"].includes(err.code || "")) {
|
|
129
|
+
message = err.message;
|
|
130
|
+
} else {
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
const code = await prompt({ method: "email", error: message });
|
|
134
|
+
if (code === null) throw { code: "LOGIN_CANCELLED", message: "Sign-in was cancelled." };
|
|
135
|
+
proof = code === "resend" ? {} : { challenge_token: challengeToken, challenge_code: code };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
9
139
|
|
|
10
140
|
// src/types/index.ts
|
|
11
141
|
var ScaleMuleApiError = class extends Error {
|
|
@@ -614,7 +744,7 @@ var ScaleMuleClient = class {
|
|
|
614
744
|
responseData = text ? JSON.parse(text) : null;
|
|
615
745
|
} catch {
|
|
616
746
|
}
|
|
617
|
-
if (!response.ok) {
|
|
747
|
+
if (!response.ok || responseData?.success === false) {
|
|
618
748
|
const rawError = responseData?.error;
|
|
619
749
|
const baseError = rawError && typeof rawError === "object" ? rawError : { code: `HTTP_${response.status}`, message: typeof rawError === "string" ? rawError : responseData?.message || text || response.statusText };
|
|
620
750
|
const error = withErrorContext(baseError, responseData, response.headers);
|
|
@@ -653,7 +783,7 @@ var ScaleMuleClient = class {
|
|
|
653
783
|
continue;
|
|
654
784
|
}
|
|
655
785
|
if (this.debug) {
|
|
656
|
-
console.error("[ScaleMule] Request failed:", error);
|
|
786
|
+
console.error("[ScaleMule] Request failed:", ["LOGIN_CHALLENGE_REQUIRED", "MFA_REQUIRED"].includes(error.code) ? error.code : error);
|
|
657
787
|
}
|
|
658
788
|
throw new ScaleMuleApiError(error);
|
|
659
789
|
}
|
|
@@ -923,6 +1053,7 @@ function setSdkTelemetryEndpoint(url) {
|
|
|
923
1053
|
endpoint = url;
|
|
924
1054
|
}
|
|
925
1055
|
function reportSdkError(payload) {
|
|
1056
|
+
if (["LOGIN_CHALLENGE_REQUIRED", "MFA_REQUIRED"].includes(payload.code)) return;
|
|
926
1057
|
if (!endpoint) return;
|
|
927
1058
|
if (typeof fetch === "undefined") return;
|
|
928
1059
|
const body = JSON.stringify({
|
|
@@ -998,8 +1129,12 @@ function ScaleMuleProvider({
|
|
|
998
1129
|
mediaPolicy,
|
|
999
1130
|
getToken,
|
|
1000
1131
|
userResolver,
|
|
1001
|
-
memberTokenPollMs
|
|
1132
|
+
memberTokenPollMs,
|
|
1133
|
+
passwordRecoveryUrl = "/auth/forgot-password",
|
|
1134
|
+
onSecurityChallenge
|
|
1002
1135
|
}) {
|
|
1136
|
+
const security = useSecurityChallenge(passwordRecoveryUrl);
|
|
1137
|
+
const requestSecurityCode = onSecurityChallenge || security.prompt;
|
|
1003
1138
|
const memberMode = typeof getToken === "function";
|
|
1004
1139
|
const [user, setUser] = useState(null);
|
|
1005
1140
|
const [initializing, setInitializing] = useState(true);
|
|
@@ -1241,6 +1376,7 @@ function ScaleMuleProvider({
|
|
|
1241
1376
|
);
|
|
1242
1377
|
const value = useMemo(
|
|
1243
1378
|
() => ({
|
|
1379
|
+
requestSecurityCode,
|
|
1244
1380
|
client,
|
|
1245
1381
|
money,
|
|
1246
1382
|
realtime: baseClient.realtime,
|
|
@@ -1268,9 +1404,12 @@ function ScaleMuleProvider({
|
|
|
1268
1404
|
accountSwitcherPrivacy,
|
|
1269
1405
|
bootstrapFlags
|
|
1270
1406
|
}),
|
|
1271
|
-
[client, money, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
|
|
1407
|
+
[requestSecurityCode, client, money, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
|
|
1272
1408
|
);
|
|
1273
|
-
return /* @__PURE__ */
|
|
1409
|
+
return /* @__PURE__ */ jsxs(ScaleMuleContext.Provider, { value, children: [
|
|
1410
|
+
children,
|
|
1411
|
+
security.element
|
|
1412
|
+
] });
|
|
1274
1413
|
}
|
|
1275
1414
|
function useScaleMule() {
|
|
1276
1415
|
const context = useContext(ScaleMuleContext);
|
|
@@ -1382,7 +1521,7 @@ async function proxyFetch(proxyUrl, path, options = {}) {
|
|
|
1382
1521
|
return data;
|
|
1383
1522
|
}
|
|
1384
1523
|
function useAuth() {
|
|
1385
|
-
const { client, user, setUser, initializing, error, setError, authProxyUrl, enableAccountSwitcher, accountSwitcherPrivacy } = useScaleMule();
|
|
1524
|
+
const { client, user, setUser, initializing, error, setError, authProxyUrl, enableAccountSwitcher, accountSwitcherPrivacy, requestSecurityCode } = useScaleMule();
|
|
1386
1525
|
const register = useCallback(
|
|
1387
1526
|
async (data) => {
|
|
1388
1527
|
setError(null);
|
|
@@ -1423,19 +1562,31 @@ function useAuth() {
|
|
|
1423
1562
|
async (data) => {
|
|
1424
1563
|
setError(null);
|
|
1425
1564
|
if (authProxyUrl) {
|
|
1426
|
-
const response = await
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
code: "LOGIN_FAILED",
|
|
1434
|
-
|
|
1435
|
-
};
|
|
1565
|
+
const response = await withAdaptiveChallenge(async (proof) => {
|
|
1566
|
+
const result = await proxyFetch(
|
|
1567
|
+
authProxyUrl,
|
|
1568
|
+
"login",
|
|
1569
|
+
{ body: { ...data, ...proof } }
|
|
1570
|
+
);
|
|
1571
|
+
if (!result.success || !result.data) {
|
|
1572
|
+
throw result.error || { code: "LOGIN_FAILED", message: "Login failed" };
|
|
1573
|
+
}
|
|
1574
|
+
return { ...result, data: result.data };
|
|
1575
|
+
}, requestSecurityCode, {
|
|
1576
|
+
send: async (pending_token, method) => {
|
|
1577
|
+
const result = await proxyFetch(authProxyUrl, "mfa/send-code", { body: { pending_token, method } });
|
|
1578
|
+
if (!result.success) throw result.error;
|
|
1579
|
+
return result;
|
|
1580
|
+
},
|
|
1581
|
+
verify: async (pending_token, code, method) => {
|
|
1582
|
+
const result = await proxyFetch(authProxyUrl, "mfa/verify", { body: { pending_token, code, method } });
|
|
1583
|
+
if (!result.success || !result.data) throw result.error;
|
|
1584
|
+
return { ...result, data: result.data };
|
|
1585
|
+
}
|
|
1586
|
+
}).catch((err) => {
|
|
1436
1587
|
setError(err);
|
|
1437
1588
|
throw err;
|
|
1438
|
-
}
|
|
1589
|
+
});
|
|
1439
1590
|
if ("requires_mfa" in response.data && response.data.requires_mfa) {
|
|
1440
1591
|
return response.data;
|
|
1441
1592
|
}
|
|
@@ -1453,7 +1604,10 @@ function useAuth() {
|
|
|
1453
1604
|
}
|
|
1454
1605
|
let loginResult;
|
|
1455
1606
|
try {
|
|
1456
|
-
loginResult = await client.post("/v1/auth/login", data)
|
|
1607
|
+
loginResult = await withAdaptiveChallenge((proof) => client.post("/v1/auth/login", { ...data, ...proof }), requestSecurityCode, {
|
|
1608
|
+
send: (pending_token, method) => client.post("/v1/auth/mfa/send-code", { pending_token, method }),
|
|
1609
|
+
verify: (pending_token, code, method) => client.post("/v1/auth/mfa/verify", { pending_token, code, method })
|
|
1610
|
+
});
|
|
1457
1611
|
} catch (err) {
|
|
1458
1612
|
if (err instanceof ScaleMuleApiError) {
|
|
1459
1613
|
setError(err);
|
|
@@ -1468,7 +1622,7 @@ function useAuth() {
|
|
|
1468
1622
|
setUser(loginData.user);
|
|
1469
1623
|
return loginData;
|
|
1470
1624
|
},
|
|
1471
|
-
[client, setUser, setError, authProxyUrl]
|
|
1625
|
+
[client, setUser, setError, authProxyUrl, requestSecurityCode]
|
|
1472
1626
|
);
|
|
1473
1627
|
const logout = useCallback(async () => {
|
|
1474
1628
|
setError(null);
|
|
@@ -1539,8 +1693,17 @@ function useAuth() {
|
|
|
1539
1693
|
throw err;
|
|
1540
1694
|
}
|
|
1541
1695
|
}
|
|
1696
|
+
await client.clearSession();
|
|
1697
|
+
setUser(null);
|
|
1698
|
+
if (typeof window !== "undefined") {
|
|
1699
|
+
const current = new URL(window.location.href);
|
|
1700
|
+
if (current.searchParams.get("token") === token) {
|
|
1701
|
+
current.searchParams.delete("token");
|
|
1702
|
+
window.history.replaceState(window.history.state, "", current.toString());
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1542
1705
|
},
|
|
1543
|
-
[client, setError, authProxyUrl]
|
|
1706
|
+
[client, setUser, setError, authProxyUrl]
|
|
1544
1707
|
);
|
|
1545
1708
|
const verifyEmail = useCallback(
|
|
1546
1709
|
async (token) => {
|
|
@@ -1821,11 +1984,15 @@ function useAuth() {
|
|
|
1821
1984
|
setError(null);
|
|
1822
1985
|
let mfaResult;
|
|
1823
1986
|
try {
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1987
|
+
if (authProxyUrl) {
|
|
1988
|
+
const result = await proxyFetch(authProxyUrl, "mfa/verify", { body: { pending_token: challengeToken, code, method } });
|
|
1989
|
+
if (!result.success || !result.data) throw result.error;
|
|
1990
|
+
const sessionToken = result.data.sessionToken || result.data.session_token;
|
|
1991
|
+
if (!sessionToken) throw new ScaleMuleApiError({ code: "MFA_FAILED", message: "Sign-in did not return a session" });
|
|
1992
|
+
mfaResult = { ...result.data, session_token: sessionToken };
|
|
1993
|
+
} else {
|
|
1994
|
+
mfaResult = await client.post("/v1/auth/mfa/verify", { pending_token: challengeToken, code, method });
|
|
1995
|
+
}
|
|
1829
1996
|
} catch (err) {
|
|
1830
1997
|
if (err instanceof ScaleMuleApiError) {
|
|
1831
1998
|
setError(err);
|
|
@@ -1836,7 +2003,7 @@ function useAuth() {
|
|
|
1836
2003
|
setUser(mfaResult.user);
|
|
1837
2004
|
return mfaResult;
|
|
1838
2005
|
},
|
|
1839
|
-
[client, setUser, setError]
|
|
2006
|
+
[client, setUser, setError, authProxyUrl]
|
|
1840
2007
|
);
|
|
1841
2008
|
const disableMFA = useCallback(
|
|
1842
2009
|
async (password) => {
|
|
@@ -3752,6 +3919,34 @@ function useRealtime(options) {
|
|
|
3752
3919
|
return { status, lastMessage, disconnect, subscribe, publish };
|
|
3753
3920
|
}
|
|
3754
3921
|
|
|
3922
|
+
// src/url-privacy.ts
|
|
3923
|
+
function withoutAuthSecrets(value) {
|
|
3924
|
+
if (!value) return value;
|
|
3925
|
+
try {
|
|
3926
|
+
const absolute = /^[a-z][a-z0-9+.-]*:/i.test(value);
|
|
3927
|
+
const protocolRelative = value.startsWith("//");
|
|
3928
|
+
const url = new URL(value, "https://relative.invalid");
|
|
3929
|
+
if (!["http:", "https:"].includes(url.protocol)) return void 0;
|
|
3930
|
+
let changed = false;
|
|
3931
|
+
for (const name of [...url.searchParams.keys()]) {
|
|
3932
|
+
if (/^(token|code|state|password|new_password|access_token|refresh_token|id_token|api_key|client_secret|challenge_token|challenge_code)$/i.test(name)) {
|
|
3933
|
+
url.searchParams.delete(name);
|
|
3934
|
+
changed = true;
|
|
3935
|
+
}
|
|
3936
|
+
}
|
|
3937
|
+
if (url.hash) {
|
|
3938
|
+
url.hash = "";
|
|
3939
|
+
changed = true;
|
|
3940
|
+
}
|
|
3941
|
+
if (!changed) return value;
|
|
3942
|
+
if (absolute) return url.toString();
|
|
3943
|
+
if (protocolRelative) return url.toString().replace(/^https:/, "");
|
|
3944
|
+
return value.split(/[?#]/, 1)[0] + url.search;
|
|
3945
|
+
} catch {
|
|
3946
|
+
return void 0;
|
|
3947
|
+
}
|
|
3948
|
+
}
|
|
3949
|
+
|
|
3755
3950
|
// src/hooks/event-dedup.ts
|
|
3756
3951
|
var DEFAULT_EVENT_DEDUP_MS = 300;
|
|
3757
3952
|
var DEDUP_MAP_MAX = 200;
|
|
@@ -4021,19 +4216,20 @@ function useAnalytics(options = {}) {
|
|
|
4021
4216
|
if (utm) setUtmParams(utm);
|
|
4022
4217
|
}
|
|
4023
4218
|
if (!landingPage.current) {
|
|
4024
|
-
landingPage.current = window.location.href;
|
|
4219
|
+
landingPage.current = withoutAuthSecrets(window.location.href) || null;
|
|
4025
4220
|
}
|
|
4026
4221
|
const storage = typeof sessionStorage !== "undefined" ? sessionStorage : void 0;
|
|
4027
4222
|
const storedReferrer = getStorageItem(storage, SESSION_REFERRER_KEY);
|
|
4028
4223
|
if (storedReferrer) {
|
|
4029
|
-
originalReferrerRef.current = storedReferrer;
|
|
4224
|
+
originalReferrerRef.current = withoutAuthSecrets(storedReferrer) || null;
|
|
4225
|
+
setStorageItem(storage, SESSION_REFERRER_KEY, originalReferrerRef.current || "");
|
|
4030
4226
|
} else if (document.referrer) {
|
|
4031
4227
|
try {
|
|
4032
4228
|
const referrerUrl = new URL(document.referrer);
|
|
4033
4229
|
const currentUrl = new URL(window.location.href);
|
|
4034
4230
|
if (referrerUrl.hostname !== currentUrl.hostname) {
|
|
4035
|
-
originalReferrerRef.current = document.referrer;
|
|
4036
|
-
setStorageItem(storage, SESSION_REFERRER_KEY, document.referrer);
|
|
4231
|
+
originalReferrerRef.current = withoutAuthSecrets(document.referrer) || null;
|
|
4232
|
+
setStorageItem(storage, SESSION_REFERRER_KEY, withoutAuthSecrets(document.referrer) || "");
|
|
4037
4233
|
}
|
|
4038
4234
|
} catch {
|
|
4039
4235
|
}
|
|
@@ -4073,10 +4269,10 @@ function useAnalytics(options = {}) {
|
|
|
4073
4269
|
session_duration_seconds: Math.floor((Date.now() - sessionStartRef.current) / 1e3)
|
|
4074
4270
|
};
|
|
4075
4271
|
if (typeof window !== "undefined") {
|
|
4076
|
-
fullEvent.page_url = window.location.href;
|
|
4272
|
+
fullEvent.page_url = withoutAuthSecrets(window.location.href);
|
|
4077
4273
|
fullEvent.page_title = document.title;
|
|
4078
4274
|
fullEvent.referrer = originalReferrerRef.current || void 0;
|
|
4079
|
-
fullEvent.document_referrer = document.referrer || void 0;
|
|
4275
|
+
fullEvent.document_referrer = withoutAuthSecrets(document.referrer) || void 0;
|
|
4080
4276
|
}
|
|
4081
4277
|
return fullEvent;
|
|
4082
4278
|
},
|
|
@@ -4159,9 +4355,9 @@ function useAnalytics(options = {}) {
|
|
|
4159
4355
|
event_category: "navigation",
|
|
4160
4356
|
properties: {
|
|
4161
4357
|
...data?.properties || {},
|
|
4162
|
-
page_url: data?.page_url || (typeof window !== "undefined" ? window.location.href : void 0),
|
|
4358
|
+
page_url: withoutAuthSecrets(data?.page_url || (typeof window !== "undefined" ? window.location.href : void 0)),
|
|
4163
4359
|
page_title: data?.page_title || (typeof document !== "undefined" ? document.title : void 0),
|
|
4164
|
-
referrer: data?.referrer || originalReferrerRef.current || void 0
|
|
4360
|
+
referrer: withoutAuthSecrets(data?.referrer || originalReferrerRef.current || void 0)
|
|
4165
4361
|
}
|
|
4166
4362
|
};
|
|
4167
4363
|
return trackEvent(pageEvent);
|
package/dist/server/auth.js
CHANGED
|
@@ -18,6 +18,34 @@ var ScaleMuleApiError = class extends Error {
|
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
// src/url-privacy.ts
|
|
22
|
+
function withoutAuthSecrets(value) {
|
|
23
|
+
if (!value) return value;
|
|
24
|
+
try {
|
|
25
|
+
const absolute = /^[a-z][a-z0-9+.-]*:/i.test(value);
|
|
26
|
+
const protocolRelative = value.startsWith("//");
|
|
27
|
+
const url = new URL(value, "https://relative.invalid");
|
|
28
|
+
if (!["http:", "https:"].includes(url.protocol)) return void 0;
|
|
29
|
+
let changed = false;
|
|
30
|
+
for (const name of [...url.searchParams.keys()]) {
|
|
31
|
+
if (/^(token|code|state|password|new_password|access_token|refresh_token|id_token|api_key|client_secret|challenge_token|challenge_code)$/i.test(name)) {
|
|
32
|
+
url.searchParams.delete(name);
|
|
33
|
+
changed = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (url.hash) {
|
|
37
|
+
url.hash = "";
|
|
38
|
+
changed = true;
|
|
39
|
+
}
|
|
40
|
+
if (!changed) return value;
|
|
41
|
+
if (absolute) return url.toString();
|
|
42
|
+
if (protocolRelative) return url.toString().replace(/^https:/, "");
|
|
43
|
+
return value.split(/[?#]/, 1)[0] + url.search;
|
|
44
|
+
} catch {
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
21
49
|
// src/server/context.ts
|
|
22
50
|
function validateIP(ip) {
|
|
23
51
|
if (!ip) return void 0;
|
|
@@ -34,8 +62,9 @@ function validateIP(ip) {
|
|
|
34
62
|
function extractClientContext(request) {
|
|
35
63
|
const headers2 = request.headers;
|
|
36
64
|
let ip;
|
|
65
|
+
ip = validateIP(headers2.get("x-real-ip"));
|
|
37
66
|
const cfConnectingIp = headers2.get("cf-connecting-ip");
|
|
38
|
-
if (cfConnectingIp) {
|
|
67
|
+
if (!ip && cfConnectingIp) {
|
|
39
68
|
ip = validateIP(cfConnectingIp);
|
|
40
69
|
}
|
|
41
70
|
if (!ip) {
|
|
@@ -75,7 +104,7 @@ function extractClientContext(request) {
|
|
|
75
104
|
}
|
|
76
105
|
const userAgent = headers2.get("user-agent") || void 0;
|
|
77
106
|
const deviceFingerprint = headers2.get("x-device-fingerprint") || void 0;
|
|
78
|
-
const referrer = headers2.get("referer") || void 0;
|
|
107
|
+
const referrer = withoutAuthSecrets(headers2.get("referer") || void 0);
|
|
79
108
|
const anonymousId = headers2.get("x-anonymous-id") || void 0;
|
|
80
109
|
return {
|
|
81
110
|
ip,
|
|
@@ -154,6 +183,12 @@ var ScaleMuleServer = class {
|
|
|
154
183
|
/**
|
|
155
184
|
* Login user - returns session token (store in HTTP-only cookie)
|
|
156
185
|
*/
|
|
186
|
+
sendMfaCode: async (pending_token, method, options) => {
|
|
187
|
+
return this.request("POST", "/v1/auth/mfa/send-code", { body: { pending_token, method }, clientContext: options?.clientContext });
|
|
188
|
+
},
|
|
189
|
+
completeMfa: async (pending_token, code, method, options) => {
|
|
190
|
+
return this.request("POST", "/v1/auth/mfa/verify", { body: { pending_token, code, method }, clientContext: options?.clientContext });
|
|
191
|
+
},
|
|
157
192
|
login: async (data, options) => {
|
|
158
193
|
return this.request("POST", "/v1/auth/login", { body: data, clientContext: options?.clientContext });
|
|
159
194
|
},
|
|
@@ -793,7 +828,7 @@ var ScaleMuleServer = class {
|
|
|
793
828
|
responseData = text ? JSON.parse(text) : null;
|
|
794
829
|
} catch {
|
|
795
830
|
}
|
|
796
|
-
if (!response.ok) {
|
|
831
|
+
if (!response.ok || responseData?.success === false) {
|
|
797
832
|
const baseError = responseData?.error || {
|
|
798
833
|
code: `HTTP_${response.status}`,
|
|
799
834
|
message: responseData?.message || text || response.statusText
|
|
@@ -1222,21 +1257,46 @@ function createAuthRoutes(config = {}) {
|
|
|
1222
1257
|
}
|
|
1223
1258
|
return registerResponse;
|
|
1224
1259
|
}
|
|
1260
|
+
case "mfa/send-code": {
|
|
1261
|
+
const { pending_token, method } = body;
|
|
1262
|
+
if (!pending_token || !["email", "sms"].includes(method)) return errorResponse("VALIDATION_ERROR", "MFA token and method required", 400);
|
|
1263
|
+
return successResponse(await sm.auth.sendMfaCode(pending_token, method, { clientContext }));
|
|
1264
|
+
}
|
|
1265
|
+
case "mfa/verify": {
|
|
1266
|
+
const { pending_token, code, method } = body;
|
|
1267
|
+
if (!pending_token || !code || !["totp", "email", "sms", "backup_code"].includes(method)) return errorResponse("VALIDATION_ERROR", "MFA token, code, and valid method required", 400);
|
|
1268
|
+
const result = await sm.auth.completeMfa(pending_token, code, method, { clientContext });
|
|
1269
|
+
if (!result.session_token || !result.user) return errorResponse("MFA_FAILED", "Unable to complete sign-in", 400);
|
|
1270
|
+
await config.onLogin?.({ id: result.user.id, email: result.user.email });
|
|
1271
|
+
const response = withSession(result, { user: result.user, sessionToken: result.session_token, userId: result.user.id }, cookieOptions);
|
|
1272
|
+
if (config.enableAccountSwitcher) {
|
|
1273
|
+
appendKnownAccountCookie(response.headers, {
|
|
1274
|
+
userId: result.user.id,
|
|
1275
|
+
email: result.user.email,
|
|
1276
|
+
fullName: result.user.full_name ?? void 0,
|
|
1277
|
+
avatarUrl: result.user.avatar_url ?? void 0,
|
|
1278
|
+
provider: "email",
|
|
1279
|
+
lastActiveAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1280
|
+
}, getKnownAccountsCookieRaw(request), cookieOptions, config.accountSwitcherPrivacy);
|
|
1281
|
+
}
|
|
1282
|
+
return response;
|
|
1283
|
+
}
|
|
1225
1284
|
// ==================== Login ====================
|
|
1226
1285
|
case "login": {
|
|
1227
|
-
const { email, password, remember_me } = body;
|
|
1286
|
+
const { email, password, remember_me, device_fingerprint, challenge_token, challenge_code } = body;
|
|
1228
1287
|
if (!email || !password) {
|
|
1229
1288
|
return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
|
|
1230
1289
|
}
|
|
1231
1290
|
let loginData;
|
|
1232
1291
|
try {
|
|
1233
|
-
loginData = await sm.auth.login({ email, password, remember_me }, { clientContext });
|
|
1292
|
+
loginData = await sm.auth.login({ email, password, remember_me, device_fingerprint, challenge_token, challenge_code }, { clientContext });
|
|
1234
1293
|
} catch (err) {
|
|
1235
1294
|
const apiErr = err instanceof ScaleMuleApiError ? err : null;
|
|
1236
1295
|
const errorCode = apiErr?.code || "LOGIN_FAILED";
|
|
1237
|
-
let status = 400;
|
|
1296
|
+
let status = ["LOGIN_CHALLENGE_REQUIRED", "MFA_REQUIRED"].includes(errorCode) ? 202 : 400;
|
|
1297
|
+
if (errorCode === "CHALLENGE_RATE_LIMITED") status = 429;
|
|
1238
1298
|
if (errorCode === "INVALID_CREDENTIALS" || errorCode === "UNAUTHORIZED") status = 401;
|
|
1239
|
-
if (["EMAIL_NOT_VERIFIED", "PHONE_NOT_VERIFIED", "ACCOUNT_LOCKED", "ACCOUNT_DISABLED"
|
|
1299
|
+
if (["EMAIL_NOT_VERIFIED", "PHONE_NOT_VERIFIED", "ACCOUNT_LOCKED", "ACCOUNT_DISABLED"].includes(errorCode)) {
|
|
1240
1300
|
status = 403;
|
|
1241
1301
|
}
|
|
1242
1302
|
return errorResponse(
|
|
@@ -1315,7 +1375,7 @@ function createAuthRoutes(config = {}) {
|
|
|
1315
1375
|
400
|
|
1316
1376
|
);
|
|
1317
1377
|
}
|
|
1318
|
-
return
|
|
1378
|
+
return clearSession({ message: "Password reset successful" }, cookieOptions);
|
|
1319
1379
|
}
|
|
1320
1380
|
// ==================== Verify Email ====================
|
|
1321
1381
|
case "verify-email": {
|
|
@@ -1508,6 +1568,10 @@ function createAuthRoutes(config = {}) {
|
|
|
1508
1568
|
return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);
|
|
1509
1569
|
}
|
|
1510
1570
|
} catch (err) {
|
|
1571
|
+
if (path.startsWith("mfa/") && err instanceof ScaleMuleApiError) {
|
|
1572
|
+
const status = ["CHALLENGE_RATE_LIMITED", "MFA_RATE_LIMITED", "MFA_MAX_ATTEMPTS"].includes(err.code) ? 429 : 400;
|
|
1573
|
+
return errorResponse(err.code, err.message, status);
|
|
1574
|
+
}
|
|
1511
1575
|
console.error("[ScaleMule Auth] Error:", err);
|
|
1512
1576
|
return errorResponse("SERVER_ERROR", "Internal server error", 500);
|
|
1513
1577
|
}
|