Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/soft-camels-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix autoconnection of inapp wallets in react native
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function ManageWalletScreen(props: {
}) {
const adminWallet = useAdminWallet();
const activeWallet = useActiveWallet();
const wallet = adminWallet || activeWallet;

return (
<Container
Expand Down Expand Up @@ -60,7 +61,7 @@ export function ManageWalletScreen(props: {
/>

{/* Unified Identity */}
{typeof activeWallet !== "undefined" &&
{typeof wallet !== "undefined" &&
props.manageWallet?.allowLinkingProfiles !== false && (
<MenuButton
onClick={() => {
Expand Down Expand Up @@ -93,9 +94,9 @@ export function ManageWalletScreen(props: {
</MenuButton>

{/* Private Key Export (if enabled) */}
{adminWallet &&
isInAppWallet(adminWallet) &&
!adminWallet.getConfig()?.hidePrivateKeyExport && (
{wallet &&
isInAppWallet(wallet) &&
!wallet.getConfig()?.hidePrivateKeyExport && (
<MenuButton
onClick={() => {
props.setScreen("private-key");
Expand Down
26 changes: 13 additions & 13 deletions packages/thirdweb/src/wallets/connection/autoConnectCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
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,
Expand All @@ -70,17 +70,17 @@
}
: 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 || [])];

Check warning on line 79 in packages/thirdweb/src/wallets/connection/autoConnectCore.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L79 was not covered by tests
}
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
Expand All @@ -105,7 +105,7 @@
wallet: activeWallet,
client: props.client,
lastConnectedChain,
authResult,
authResult: result?.authResult,
}),
{
ms: timeout,
Expand Down Expand Up @@ -156,7 +156,7 @@
wallet,
client: props.client,
lastConnectedChain,
authResult,
authResult: result?.authResult,
});
manager.addConnectedWallet(wallet);
} catch {
Expand Down
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand All @@ -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", () => {
Expand Down
20 changes: 11 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 @@ -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;
Expand All @@ -40,5 +42,5 @@ export function getUrlToken(): {
);
return { walletId, authResult, authProvider, authCookie };
}
return {};
return undefined;
}
Loading