Skip to content

Commit 3f7a071

Browse files
Preserve custom URL params when removing thirdweb authentication params
Co-authored-by: joaquim.verges <[email protected]>
1 parent e1ad7da commit 3f7a071

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.test.tsx

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ describe.runIf(global.window !== undefined)("getUrlToken", () => {
6767
walletId: "123",
6868
});
6969

70-
// Check if URL has been updated correctly
71-
expect(window.location.search).toBe("?walletId=123&authCookie=myCookie");
70+
// Check if URL has been updated correctly (should remove thirdweb params)
71+
expect(window.location.search).toBe("");
7272
});
7373

7474
it("should handle all parameters correctly", () => {
@@ -84,9 +84,52 @@ describe.runIf(global.window !== undefined)("getUrlToken", () => {
8484
walletId: "123",
8585
});
8686

87-
// Check if URL has been updated correctly
88-
expect(window.location.search).toBe(
89-
"?walletId=123&authResult=%7B%22token%22%3A%22xyz%22%7D&authProvider=provider1&authCookie=myCookie",
90-
);
87+
// Check if URL has been updated correctly (should remove all thirdweb params)
88+
expect(window.location.search).toBe("");
89+
});
90+
91+
it("should preserve custom parameters while removing thirdweb ones", () => {
92+
window.location.search =
93+
"?custom_param=value&walletId=123&another_custom=test&authCookie=myCookie&user_id=456";
94+
95+
const result = getUrlToken();
96+
97+
expect(result).toEqual({
98+
authCookie: "myCookie",
99+
authProvider: null,
100+
authResult: undefined,
101+
walletId: "123",
102+
});
103+
104+
// Check if custom parameters are preserved while thirdweb ones are removed
105+
expect(window.location.search).toBe("?custom_param=value&another_custom=test&user_id=456");
106+
});
107+
108+
it("should preserve custom parameters with all thirdweb parameters", () => {
109+
window.location.search =
110+
"?utm_source=google&walletId=123&authResult=%7B%22token%22%3A%22xyz%22%7D&authProvider=provider1&authCookie=myCookie&ref=homepage";
111+
112+
const result = getUrlToken();
113+
114+
expect(result).toEqual({
115+
authCookie: "myCookie",
116+
authProvider: "provider1",
117+
authResult: { token: "xyz" },
118+
walletId: "123",
119+
});
120+
121+
// Check if custom parameters (utm_source, ref) are preserved
122+
expect(window.location.search).toBe("?utm_source=google&ref=homepage");
123+
});
124+
125+
it("should handle case where only custom parameters exist", () => {
126+
window.location.search = "?custom_param=value&another_param=test";
127+
128+
const result = getUrlToken();
129+
130+
expect(result).toEqual(undefined);
131+
132+
// URL should remain unchanged when no thirdweb params are present
133+
expect(window.location.search).toBe("?custom_param=value&another_param=test");
91134
});
92135
});

packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ export function getUrlToken():
2828
if ((authCookie || authResultString) && walletId) {
2929
const authResult = (() => {
3030
if (authResultString) {
31-
params.delete("authResult");
3231
return JSON.parse(decodeURIComponent(authResultString));
3332
}
3433
})();
35-
params.delete("walletId");
36-
params.delete("authProvider");
37-
params.delete("authCookie");
38-
window.history.pushState(
39-
{},
40-
"",
41-
`${window.location.pathname}?${params.toString()}`,
42-
);
34+
35+
// Only remove thirdweb-specific parameters, preserving custom ones
36+
const thirdwebParams = ["authResult", "walletId", "authProvider", "authCookie"];
37+
const updatedParams = new URLSearchParams(queryString);
38+
39+
thirdwebParams.forEach(param => {
40+
updatedParams.delete(param);
41+
});
42+
43+
// Update the URL with only the custom parameters preserved
44+
const newUrl = updatedParams.toString()
45+
? `${window.location.pathname}?${updatedParams.toString()}`
46+
: window.location.pathname;
47+
48+
window.history.pushState({}, "", newUrl);
49+
4350
return { authCookie, authProvider, authResult, walletId };
4451
}
4552
return undefined;

0 commit comments

Comments
 (0)