11"use client" ;
22
33import { useQuery } from "@tanstack/react-query" ;
4- import type { Chain } from "../../../../chains/types.js" ;
5- import type { ThirdwebClient } from "../../../../client/client.js" ;
64import type { AsyncStorage } from "../../../../utils/storage/AsyncStorage.js" ;
7- import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js" ;
8- import { ClientScopedStorage } from "../../../../wallets/in-app/core/authentication/client-scoped-storage.js" ;
9- import type { AuthStoredTokenWithCookieReturnType } from "../../../../wallets/in-app/core/authentication/types.js" ;
10- import { getUrlToken } from "../../../../wallets/in-app/web/lib/get-url-token.js" ;
5+ import { autoConnectCore } from "../../../../wallets/connection/autoConnectCore.js" ;
6+ import type { AutoConnectProps } from "../../../../wallets/connection/types.js" ;
117import type { Wallet } from "../../../../wallets/interfaces/wallet.js" ;
12- import {
13- getLastConnectedChain ,
14- getStoredActiveWalletId ,
15- getStoredConnectedWalletIds ,
16- } from "../../../../wallets/manager/index.js" ;
178import type { WalletId } from "../../../../wallets/wallet-types.js" ;
189import { useConnectionManagerCtx } from "../../providers/connection-manager.js" ;
1910import { setLastAuthProvider } from "../../utils/storage.js" ;
20- import { timeoutPromise } from "../../utils/timeoutPromise.js" ;
21- import type { AutoConnectProps } from "../connection/types.js" ;
2211import { useConnect } from "./useConnect.js" ;
23- import { useSetActiveWalletConnectionStatus } from "./useSetActiveWalletConnectionStatus.js" ;
2412
2513export function useAutoConnectCore (
2614 storage : AsyncStorage ,
@@ -29,161 +17,27 @@ export function useAutoConnectCore(
2917 getInstalledWallets ?: ( ) => Wallet [ ] ,
3018) {
3119 const manager = useConnectionManagerCtx ( "useAutoConnect" ) ;
32- const setConnectionStatus = useSetActiveWalletConnectionStatus ( ) ;
3320 const { connect } = useConnect ( {
3421 client : props . client ,
3522 accountAbstraction : props . accountAbstraction ,
3623 } ) ;
37- const { isAutoConnecting } = manager ;
38- const { wallets, onConnect } = props ;
39- const timeout = props . timeout ?? 15000 ;
40- // get the supported wallets from thirdweb provider
41- // check the storage for last connected wallets and connect them all
42- // check the storage for last active wallet and set it as active
43- const autoConnect = async ( ) : Promise < boolean > => {
44- let autoConnected = false ;
45- isAutoConnecting . setValue ( true ) ;
46- let [ lastConnectedWalletIds , lastActiveWalletId ] = await Promise . all ( [
47- getStoredConnectedWalletIds ( storage ) ,
48- getStoredActiveWalletId ( storage ) ,
49- ] ) ;
50-
51- const { authResult, walletId, authProvider, authCookie } = getUrlToken ( ) ;
52- const wallet = wallets . find ( ( w ) => w . id === walletId ) ;
53-
54- // If an auth cookie is found and this site supports the wallet, we'll set the auth cookie in the client storage
55- if ( authCookie && wallet ) {
56- const clientStorage = new ClientScopedStorage ( {
57- storage,
58- clientId : props . client . clientId ,
59- ecosystem : isEcosystemWallet ( wallet )
60- ? {
61- id : wallet . id ,
62- partnerId : wallet . getConfig ( ) ?. partnerId ,
63- }
64- : undefined ,
65- } ) ;
66- await clientStorage . saveAuthCookie ( authCookie ) ;
67- }
68-
69- if ( walletId ) {
70- lastActiveWalletId = walletId ;
71- lastConnectedWalletIds = lastConnectedWalletIds ?. includes ( walletId )
72- ? lastConnectedWalletIds
73- : [ walletId , ...( lastConnectedWalletIds || [ ] ) ] ;
74- }
75- if ( authProvider ) {
76- await setLastAuthProvider ( authProvider , storage ) ;
77- }
78-
79- // if no wallets were last connected or we didn't receive an auth token
80- if ( ! lastConnectedWalletIds ) {
81- return autoConnected ;
82- }
83-
84- // this flow can actually be used for a first connection in the case of a redirect
85- // in that case, we default to the passed chain to connect to
86- const lastConnectedChain =
87- ( await getLastConnectedChain ( storage ) ) || props . chain ;
88-
89- const availableWallets = [ ...wallets , ...( getInstalledWallets ?.( ) ?? [ ] ) ] ;
90- const activeWallet =
91- lastActiveWalletId &&
92- ( availableWallets . find ( ( w ) => w . id === lastActiveWalletId ) ||
93- createWalletFn ( lastActiveWalletId ) ) ;
94-
95- if ( activeWallet ) {
96- try {
97- setConnectionStatus ( "connecting" ) ; // only set connecting status if we are connecting the last active EOA
98- await timeoutPromise (
99- handleWalletConnection ( {
100- wallet : activeWallet ,
101- client : props . client ,
102- lastConnectedChain,
103- authResult,
104- } ) ,
105- {
106- ms : timeout ,
107- message : `AutoConnect timeout: ${ timeout } ms limit exceeded.` ,
108- } ,
109- ) . catch ( ( err ) => {
110- console . warn ( err . message ) ;
111- if ( props . onTimeout ) {
112- props . onTimeout ( ) ;
113- }
114- } ) ;
115-
116- // connected wallet could be activeWallet or smart wallet
117- const connectedWallet = await connect ( activeWallet ) ;
118-
119- if ( connectedWallet ) {
120- if ( onConnect ) {
121- try {
122- onConnect ( connectedWallet ) ;
123- autoConnected = true ;
124- } catch {
125- // ignore
126- }
127- }
128- } else {
129- setConnectionStatus ( "disconnected" ) ;
130- }
131- } catch ( e ) {
132- if ( e instanceof Error ) {
133- console . warn ( "Error auto connecting wallet:" , e . message ) ;
134- }
135- setConnectionStatus ( "disconnected" ) ;
136- }
137- } else {
138- setConnectionStatus ( "disconnected" ) ;
139- }
140-
141- // then connect wallets that were last connected but were not set as active
142- const otherWallets = availableWallets . filter (
143- ( w ) =>
144- w . id !== lastActiveWalletId && lastConnectedWalletIds . includes ( w . id ) ,
145- ) ;
146-
147- for ( const wallet of otherWallets ) {
148- try {
149- await handleWalletConnection ( {
150- wallet,
151- client : props . client ,
152- lastConnectedChain,
153- authResult,
154- } ) ;
155- manager . addConnectedWallet ( wallet ) ;
156- } catch {
157- // no-op
158- }
159- }
160- isAutoConnecting . setValue ( false ) ;
161- return autoConnected ; // useQuery needs a return value
162- } ;
16324
16425 // trigger the auto connect on first mount only
16526 const query = useQuery ( {
16627 queryKey : [ "autoConnect" , props . client . clientId ] ,
167- queryFn : autoConnect ,
28+ queryFn : ( ) =>
29+ autoConnectCore ( {
30+ createWalletFn,
31+ manager,
32+ props,
33+ storage,
34+ connectOverride : connect ,
35+ getInstalledWallets,
36+ setLastAuthProvider,
37+ } ) ,
16838 refetchOnMount : false ,
16939 refetchOnWindowFocus : false ,
17040 } ) ;
17141
17242 return query ;
17343}
174-
175- /**
176- * @internal
177- */
178- export async function handleWalletConnection ( props : {
179- wallet : Wallet ;
180- client : ThirdwebClient ;
181- authResult : AuthStoredTokenWithCookieReturnType | undefined ;
182- lastConnectedChain : Chain | undefined ;
183- } ) {
184- return props . wallet . autoConnect ( {
185- client : props . client ,
186- chain : props . lastConnectedChain ,
187- authResult : props . authResult ,
188- } ) ;
189- }
0 commit comments