Skip to content

Commit e9d0b6e

Browse files
committed
SDK: do not attempt autoconnect again if already successfull (#6900)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR introduces a new `force` option for the auto-connect functionality in the `thirdweb` wallet, allowing users to force an auto-connect attempt even if a previous attempt was successful. ### Detailed summary - Added `force` option to `AutoConnectProps` in `autoConnect.ts`. - Updated `autoConnectCore` to respect the `force` option, allowing multiple connect attempts. - Modified tests in `autoConnectCore.test.ts` to include the `force` option in various scenarios. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent ee065b8 commit e9d0b6e

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

.changeset/odd-rooms-sleep.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+
Only attempt autoconnect once

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ import type { AutoConnectProps } from "./types.js";
3232
export async function autoConnect(
3333
props: AutoConnectProps & {
3434
wallets?: Wallet[];
35+
/**
36+
* If true, the auto connect will be forced even if autoConnect has already been attempted successfully earlier.
37+
*
38+
* @default `false`
39+
*/
40+
force?: boolean;
3541
},
3642
): Promise<boolean> {
3743
const wallets = props.wallets || getDefaultWallets(props);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe("useAutoConnectCore", () => {
3535

3636
expect(
3737
await autoConnectCore({
38+
force: true,
3839
storage: mockStorage,
3940
props: {
4041
wallets: [wallet],
@@ -68,6 +69,7 @@ describe("useAutoConnectCore", () => {
6869

6970
expect(
7071
await autoConnectCore({
72+
force: true,
7173
storage: mockStorage,
7274
props: {
7375
wallets: [wallet],
@@ -111,6 +113,7 @@ describe("useAutoConnectCore", () => {
111113
});
112114

113115
await autoConnectCore({
116+
force: true,
114117
storage: mockStorage,
115118
props: {
116119
wallets: [wallet],
@@ -155,6 +158,7 @@ describe("useAutoConnectCore", () => {
155158
});
156159

157160
await autoConnectCore({
161+
force: true,
158162
storage: mockStorage,
159163
props: {
160164
wallets: [wallet],
@@ -191,6 +195,7 @@ describe("useAutoConnectCore", () => {
191195
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
192196

193197
await autoConnectCore({
198+
force: true,
194199
storage: mockStorage,
195200
props: {
196201
wallets: [wallet1],
@@ -235,6 +240,7 @@ describe("useAutoConnectCore", () => {
235240
const addConnectedWalletSpy = vi.spyOn(manager, "addConnectedWallet");
236241

237242
await autoConnectCore({
243+
force: true,
238244
storage: mockStorage,
239245
props: {
240246
wallets: [wallet1, wallet2],
@@ -264,6 +270,7 @@ describe("useAutoConnectCore", () => {
264270
JSON.stringify([wallet.id]),
265271
);
266272
await autoConnectCore({
273+
force: true,
267274
storage: mockStorage,
268275
props: {
269276
wallets: [wallet],
@@ -276,6 +283,7 @@ describe("useAutoConnectCore", () => {
276283

277284
expect(mockOnConnect).toHaveBeenCalledWith(wallet);
278285
});
286+
279287
it("should continue even if onConnect callback throws", async () => {
280288
const mockOnConnect = vi.fn();
281289
mockOnConnect.mockImplementation(() => {
@@ -296,6 +304,7 @@ describe("useAutoConnectCore", () => {
296304
JSON.stringify([wallet.id]),
297305
);
298306
await autoConnectCore({
307+
force: true,
299308
storage: mockStorage,
300309
props: {
301310
wallets: [wallet],
@@ -308,6 +317,7 @@ describe("useAutoConnectCore", () => {
308317

309318
expect(mockOnConnect).toHaveBeenCalledWith(wallet);
310319
});
320+
311321
it("should call setLastAuthProvider if authProvider is present", async () => {
312322
const wallet = createWalletAdapter({
313323
adaptedAccount: TEST_ACCOUNT_A,
@@ -328,6 +338,7 @@ describe("useAutoConnectCore", () => {
328338
JSON.stringify([wallet.id]),
329339
);
330340
await autoConnectCore({
341+
force: true,
331342
storage: mockStorage,
332343
props: {
333344
wallets: [wallet],
@@ -340,6 +351,7 @@ describe("useAutoConnectCore", () => {
340351

341352
expect(mockSetLastAuthProvider).toHaveBeenCalledWith("email", mockStorage);
342353
});
354+
343355
it("should set connection status to disconnect if no connectedWallet is returned", async () => {
344356
const wallet = createWalletAdapter({
345357
adaptedAccount: TEST_ACCOUNT_A,
@@ -360,6 +372,7 @@ describe("useAutoConnectCore", () => {
360372
.mockResolvedValueOnce(null as unknown as Wallet);
361373

362374
await autoConnectCore({
375+
force: true,
363376
storage: mockStorage,
364377
props: {
365378
wallets: [wallet],

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

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,7 @@ import {
1919
import type { WalletId } from "../wallet-types.js";
2020
import type { AutoConnectProps } from "./types.js";
2121

22-
/**
23-
* @internal
24-
*/
25-
export const autoConnectCore = async ({
26-
storage,
27-
props,
28-
createWalletFn,
29-
manager,
30-
connectOverride,
31-
getInstalledWallets,
32-
setLastAuthProvider,
33-
}: {
22+
type AutoConnectCoreProps = {
3423
storage: AsyncStorage;
3524
props: AutoConnectProps & { wallets: Wallet[] };
3625
createWalletFn: (id: WalletId) => Wallet;
@@ -43,7 +32,45 @@ export const autoConnectCore = async ({
4332
authProvider: AuthArgsType["strategy"],
4433
storage: AsyncStorage,
4534
) => Promise<void>;
46-
}): Promise<boolean> => {
35+
/**
36+
* If true, the auto connect will be forced even if autoConnect has already been attempted successfully earlier.
37+
*
38+
* @default `false`
39+
*/
40+
force?: boolean;
41+
};
42+
43+
let lastAutoConnectionResultPromise: Promise<boolean> | undefined = undefined;
44+
45+
/**
46+
* @internal
47+
*/
48+
export const autoConnectCore = async (props: AutoConnectCoreProps) => {
49+
// if an auto connect was attempted already
50+
if (lastAutoConnectionResultPromise && !props.force) {
51+
// wait for its resolution
52+
const lastResult = await lastAutoConnectionResultPromise;
53+
// if it was successful, return true
54+
// if not continue with the new auto connect
55+
if (lastResult) {
56+
return true;
57+
}
58+
}
59+
60+
const resultPromise = _autoConnectCore(props);
61+
lastAutoConnectionResultPromise = resultPromise;
62+
return resultPromise;
63+
};
64+
65+
const _autoConnectCore = async ({
66+
storage,
67+
props,
68+
createWalletFn,
69+
manager,
70+
connectOverride,
71+
getInstalledWallets,
72+
setLastAuthProvider,
73+
}: AutoConnectCoreProps): Promise<boolean> => {
4774
const { wallets, onConnect } = props;
4875
const timeout = props.timeout ?? 15000;
4976

0 commit comments

Comments
 (0)