Skip to content

Preserve custom url parameters in autoconnect #7778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ describe.runIf(global.window !== undefined)("getUrlToken", () => {
walletId: "123",
});

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

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

// Check if URL has been updated correctly
expect(window.location.search).toBe(
"?walletId=123&authResult=%7B%22token%22%3A%22xyz%22%7D&authProvider=provider1&authCookie=myCookie",
);
// Check if URL has been updated correctly (should remove all thirdweb params)
expect(window.location.search).toBe("");
});

it("should preserve custom parameters while removing thirdweb ones", () => {
window.location.search =
"?custom_param=value&walletId=123&another_custom=test&authCookie=myCookie&user_id=456";

const result = getUrlToken();

expect(result).toEqual({
authCookie: "myCookie",
authProvider: null,
authResult: undefined,
walletId: "123",
});

// Check if custom parameters are preserved while thirdweb ones are removed
expect(window.location.search).toBe("?custom_param=value&another_custom=test&user_id=456");
});

it("should preserve custom parameters with all thirdweb parameters", () => {
window.location.search =
"?utm_source=google&walletId=123&authResult=%7B%22token%22%3A%22xyz%22%7D&authProvider=provider1&authCookie=myCookie&ref=homepage";

const result = getUrlToken();

expect(result).toEqual({
authCookie: "myCookie",
authProvider: "provider1",
authResult: { token: "xyz" },
walletId: "123",
});

// Check if custom parameters (utm_source, ref) are preserved
expect(window.location.search).toBe("?utm_source=google&ref=homepage");
});

it("should handle case where only custom parameters exist", () => {
window.location.search = "?custom_param=value&another_param=test";

const result = getUrlToken();

expect(result).toEqual(undefined);

// URL should remain unchanged when no thirdweb params are present
expect(window.location.search).toBe("?custom_param=value&another_param=test");
});
});
25 changes: 16 additions & 9 deletions packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ export function getUrlToken():
if ((authCookie || authResultString) && walletId) {
const authResult = (() => {
if (authResultString) {
params.delete("authResult");
return JSON.parse(decodeURIComponent(authResultString));
}
})();
params.delete("walletId");
params.delete("authProvider");
params.delete("authCookie");
window.history.pushState(
{},
"",
`${window.location.pathname}?${params.toString()}`,
);

// Only remove thirdweb-specific parameters, preserving custom ones
const thirdwebParams = ["authResult", "walletId", "authProvider", "authCookie"];
const updatedParams = new URLSearchParams(queryString);

thirdwebParams.forEach(param => {
updatedParams.delete(param);
});

// Update the URL with only the custom parameters preserved
const newUrl = updatedParams.toString()
? `${window.location.pathname}?${updatedParams.toString()}`
: window.location.pathname;

window.history.pushState({}, "", newUrl);

return { authCookie, authProvider, authResult, walletId };
}
return undefined;
Expand Down
Loading