diff --git a/.changeset/soft-camels-beg.md b/.changeset/soft-camels-beg.md new file mode 100644 index 00000000000..42b9988942b --- /dev/null +++ b/.changeset/soft-camels-beg.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Fix autoconnection of inapp wallets in react native diff --git a/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/ManageWalletScreen.tsx b/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/ManageWalletScreen.tsx index 73d1036a999..240f120c567 100644 --- a/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/ManageWalletScreen.tsx +++ b/packages/thirdweb/src/react/web/ui/ConnectWallet/screens/ManageWalletScreen.tsx @@ -30,6 +30,7 @@ export function ManageWalletScreen(props: { }) { const adminWallet = useAdminWallet(); const activeWallet = useActiveWallet(); + const wallet = adminWallet || activeWallet; return ( {/* Unified Identity */} - {typeof activeWallet !== "undefined" && + {typeof wallet !== "undefined" && props.manageWallet?.allowLinkingProfiles !== false && ( { @@ -93,9 +94,9 @@ export function ManageWalletScreen(props: { {/* Private Key Export (if enabled) */} - {adminWallet && - isInAppWallet(adminWallet) && - !adminWallet.getConfig()?.hidePrivateKeyExport && ( + {wallet && + isInAppWallet(wallet) && + !wallet.getConfig()?.hidePrivateKeyExport && ( { props.setScreen("private-key"); diff --git a/packages/thirdweb/src/wallets/connection/autoConnectCore.ts b/packages/thirdweb/src/wallets/connection/autoConnectCore.ts index befc866c9b8..e12489a470f 100644 --- a/packages/thirdweb/src/wallets/connection/autoConnectCore.ts +++ b/packages/thirdweb/src/wallets/connection/autoConnectCore.ts @@ -55,11 +55,11 @@ export const autoConnectCore = async ({ getStoredActiveWalletId(storage), ]); - const { authResult, walletId, authProvider, authCookie } = getUrlToken(); - const wallet = wallets.find((w) => w.id === walletId); + const result = getUrlToken(); // If an auth cookie is found and this site supports the wallet, we'll set the auth cookie in the client storage - if (authCookie && wallet) { + const wallet = wallets.find((w) => w.id === result?.walletId); + if (result?.authCookie && wallet) { const clientStorage = new ClientScopedStorage({ storage, clientId: props.client.clientId, @@ -70,17 +70,17 @@ export const autoConnectCore = async ({ } : undefined, }); - await clientStorage.saveAuthCookie(authCookie); + await clientStorage.saveAuthCookie(result.authCookie); } - - if (walletId) { - lastActiveWalletId = walletId; - lastConnectedWalletIds = lastConnectedWalletIds?.includes(walletId) + if (result?.walletId) { + lastActiveWalletId = result.walletId; + lastConnectedWalletIds = lastConnectedWalletIds?.includes(result.walletId) ? lastConnectedWalletIds - : [walletId, ...(lastConnectedWalletIds || [])]; + : [result.walletId, ...(lastConnectedWalletIds || [])]; } - if (authProvider) { - await setLastAuthProvider?.(authProvider, storage); + + if (result?.authProvider) { + await setLastAuthProvider?.(result.authProvider, storage); } // if no wallets were last connected or we didn't receive an auth token @@ -105,7 +105,7 @@ export const autoConnectCore = async ({ wallet: activeWallet, client: props.client, lastConnectedChain, - authResult, + authResult: result?.authResult, }), { ms: timeout, @@ -156,7 +156,7 @@ export const autoConnectCore = async ({ wallet, client: props.client, lastConnectedChain, - authResult, + authResult: result?.authResult, }); manager.addConnectedWallet(wallet); } catch { diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.test.tsx b/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.test.tsx index a6fe356d0e7..2b8febadce5 100644 --- a/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.test.tsx +++ b/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.test.tsx @@ -1,7 +1,7 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { getUrlToken } from "./get-url-token.js"; -describe("getUrlToken", () => { +describe.runIf(global.window !== undefined)("getUrlToken", () => { let originalLocation: Location; beforeEach(() => { @@ -25,20 +25,20 @@ describe("getUrlToken", () => { }); it("should return an empty object if not in web context", () => { - const originalWindow = window; + const originalDocument = document; // biome-ignore lint/suspicious/noExplicitAny: Test - (global as any).window = undefined; + (global as any).document = undefined; const result = getUrlToken(); // biome-ignore lint/suspicious/noExplicitAny: Test - (global as any).window = originalWindow; + (global as any).document = originalDocument; - expect(result).toEqual({}); + expect(result).toEqual(undefined); }); it("should return an empty object if no parameters are present", () => { const result = getUrlToken(); - expect(result).toEqual({}); + expect(result).toEqual(undefined); }); it("should parse walletId and authResult correctly", () => { diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.ts b/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.ts index 3a8d1ca954a..c6aacf0fc86 100644 --- a/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.ts +++ b/packages/thirdweb/src/wallets/in-app/web/lib/get-url-token.ts @@ -5,15 +5,17 @@ import type { AuthStoredTokenWithCookieReturnType } from "../../core/authenticat /** * Checks for an auth token and associated metadata in the current URL */ -export function getUrlToken(): { - walletId?: WalletId; - authResult?: AuthStoredTokenWithCookieReturnType; - authProvider?: AuthOption; - authCookie?: string; -} { - if (typeof window === "undefined") { +export function getUrlToken(): + | { + walletId?: WalletId; + authResult?: AuthStoredTokenWithCookieReturnType; + authProvider?: AuthOption; + authCookie?: string; + } + | undefined { + if (typeof document === "undefined") { // Not in web - return {}; + return undefined; } const queryString = window.location.search; @@ -40,5 +42,5 @@ export function getUrlToken(): { ); return { walletId, authResult, authProvider, authCookie }; } - return {}; + return undefined; }