Skip to content

Commit 1401f8d

Browse files
[SDK] fix: Auto-connection of in-app wallets in React Native (#6076)
1 parent 1616b7f commit 1401f8d

File tree

5 files changed

+40
-32
lines changed

5 files changed

+40
-32
lines changed

.changeset/soft-camels-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix autoconnection of inapp wallets in react native

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/ManageWalletScreen.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function ManageWalletScreen(props: {
3030
}) {
3131
const adminWallet = useAdminWallet();
3232
const activeWallet = useActiveWallet();
33+
const wallet = adminWallet || activeWallet;
3334

3435
return (
3536
<Container
@@ -60,7 +61,7 @@ export function ManageWalletScreen(props: {
6061
/>
6162

6263
{/* Unified Identity */}
63-
{typeof activeWallet !== "undefined" &&
64+
{typeof wallet !== "undefined" &&
6465
props.manageWallet?.allowLinkingProfiles !== false && (
6566
<MenuButton
6667
onClick={() => {
@@ -93,9 +94,9 @@ export function ManageWalletScreen(props: {
9394
</MenuButton>
9495

9596
{/* Private Key Export (if enabled) */}
96-
{adminWallet &&
97-
isInAppWallet(adminWallet) &&
98-
!adminWallet.getConfig()?.hidePrivateKeyExport && (
97+
{wallet &&
98+
isInAppWallet(wallet) &&
99+
!wallet.getConfig()?.hidePrivateKeyExport && (
99100
<MenuButton
100101
onClick={() => {
101102
props.setScreen("private-key");

packages/thirdweb/src/wallets/connection/autoConnectCore.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ export const autoConnectCore = async ({
5555
getStoredActiveWalletId(storage),
5656
]);
5757

58-
const { authResult, walletId, authProvider, authCookie } = getUrlToken();
59-
const wallet = wallets.find((w) => w.id === walletId);
58+
const result = getUrlToken();
6059

6160
// If an auth cookie is found and this site supports the wallet, we'll set the auth cookie in the client storage
62-
if (authCookie && wallet) {
61+
const wallet = wallets.find((w) => w.id === result?.walletId);
62+
if (result?.authCookie && wallet) {
6363
const clientStorage = new ClientScopedStorage({
6464
storage,
6565
clientId: props.client.clientId,
@@ -70,17 +70,17 @@ export const autoConnectCore = async ({
7070
}
7171
: undefined,
7272
});
73-
await clientStorage.saveAuthCookie(authCookie);
73+
await clientStorage.saveAuthCookie(result.authCookie);
7474
}
75-
76-
if (walletId) {
77-
lastActiveWalletId = walletId;
78-
lastConnectedWalletIds = lastConnectedWalletIds?.includes(walletId)
75+
if (result?.walletId) {
76+
lastActiveWalletId = result.walletId;
77+
lastConnectedWalletIds = lastConnectedWalletIds?.includes(result.walletId)
7978
? lastConnectedWalletIds
80-
: [walletId, ...(lastConnectedWalletIds || [])];
79+
: [result.walletId, ...(lastConnectedWalletIds || [])];
8180
}
82-
if (authProvider) {
83-
await setLastAuthProvider?.(authProvider, storage);
81+
82+
if (result?.authProvider) {
83+
await setLastAuthProvider?.(result.authProvider, storage);
8484
}
8585

8686
// if no wallets were last connected or we didn't receive an auth token
@@ -105,7 +105,7 @@ export const autoConnectCore = async ({
105105
wallet: activeWallet,
106106
client: props.client,
107107
lastConnectedChain,
108-
authResult,
108+
authResult: result?.authResult,
109109
}),
110110
{
111111
ms: timeout,
@@ -156,7 +156,7 @@ export const autoConnectCore = async ({
156156
wallet,
157157
client: props.client,
158158
lastConnectedChain,
159-
authResult,
159+
authResult: result?.authResult,
160160
});
161161
manager.addConnectedWallet(wallet);
162162
} catch {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it } from "vitest";
22
import { getUrlToken } from "./get-url-token.js";
33

4-
describe("getUrlToken", () => {
4+
describe.runIf(global.window !== undefined)("getUrlToken", () => {
55
let originalLocation: Location;
66

77
beforeEach(() => {
@@ -25,20 +25,20 @@ describe("getUrlToken", () => {
2525
});
2626

2727
it("should return an empty object if not in web context", () => {
28-
const originalWindow = window;
28+
const originalDocument = document;
2929
// biome-ignore lint/suspicious/noExplicitAny: Test
30-
(global as any).window = undefined;
30+
(global as any).document = undefined;
3131

3232
const result = getUrlToken();
3333
// biome-ignore lint/suspicious/noExplicitAny: Test
34-
(global as any).window = originalWindow;
34+
(global as any).document = originalDocument;
3535

36-
expect(result).toEqual({});
36+
expect(result).toEqual(undefined);
3737
});
3838

3939
it("should return an empty object if no parameters are present", () => {
4040
const result = getUrlToken();
41-
expect(result).toEqual({});
41+
expect(result).toEqual(undefined);
4242
});
4343

4444
it("should parse walletId and authResult correctly", () => {

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import type { AuthStoredTokenWithCookieReturnType } from "../../core/authenticat
55
/**
66
* Checks for an auth token and associated metadata in the current URL
77
*/
8-
export function getUrlToken(): {
9-
walletId?: WalletId;
10-
authResult?: AuthStoredTokenWithCookieReturnType;
11-
authProvider?: AuthOption;
12-
authCookie?: string;
13-
} {
14-
if (typeof window === "undefined") {
8+
export function getUrlToken():
9+
| {
10+
walletId?: WalletId;
11+
authResult?: AuthStoredTokenWithCookieReturnType;
12+
authProvider?: AuthOption;
13+
authCookie?: string;
14+
}
15+
| undefined {
16+
if (typeof document === "undefined") {
1517
// Not in web
16-
return {};
18+
return undefined;
1719
}
1820

1921
const queryString = window.location.search;
@@ -40,5 +42,5 @@ export function getUrlToken(): {
4042
);
4143
return { walletId, authResult, authProvider, authCookie };
4244
}
43-
return {};
45+
return undefined;
4446
}

0 commit comments

Comments
 (0)