wagmi: bug: autoConnect fails to reconnect
Is there an existing issue for this?
- I have searched the existing issues
Package Version
1.1.0
Current Behavior
I am using @wagmi/core in a SvelteKit project. I am using the InjectedConnector to connect an injected wallet.
With an easier version (0.10.10), the autoConnect feature worked as expected – if the user had already connected their injected wallet and returned to the site, the wallet would auto-reconnect.
After upgrading to 1.x, this feature stopped working.
See #2439 for additional details / discussion.
Expected Behavior
When using autoConnect, the injected wallet automatically reconnects when a user returns to the site (or just on page-refresh).
Steps To Reproduce
See the linked minimal repro for a more detailed example.
- Configure wagmi with
autoConnect: true:
const { publicClient, webSocketPublicClient } = configureChains(
[mainnet],
[publicProvider()]
);
createConfig({
autoConnect: true,
publicClient,
webSocketPublicClient,
});
- Connect a wallet using
InjectedConnector:
connect({ connector: new InjectedConnector() });
- Refresh page, note that wallet doesn’t reconnect.
Link to Minimal Reproducible Example (StackBlitz, CodeSandbox, GitHub repo etc.)
https://github.com/kenkunz/wagmi-autoconnect-repro
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 2
- Comments: 17 (2 by maintainers)
Thanks @glitch-txs for your diagnosis / explanation, and @tmm for further clarification.
I just wanted to note that the Getting Started guide perfectly demonstrates this issue (my minimal repro follows the guide line-for-line). I think it would really help if the guide were updated to reflect a working scenario (especially since
autoConnect: trueis explicitly configured there).Additionally, this behavior is a departure from
v0.10.x, so I would recommend it be listed as a1.x.xbreaking change in the Migration Guide.@tmm let me know if you agree – I would be happy to submit a PR with the above documentation improvements.
So I just tried using Wagmi for the first time, and the quick tutorial on the home page is still not updated. I came here to report the same issue. I use NextJS 13.4.2 and I thought that this was an issue with Next/React.
Here is my band aid solution:
I also removed the
autoConnect: truewithin the _app.tsx file where the createConfig is. Funny enough, this resulted in fixing the hydration issue too.No idea why this issue is closed since the problem is still there.
@glitch-txs is correct! Since the connector doesn’t have
storagedefined, it can’t read from and write to storage. The easiest way to set up the storage property for connectors is to “register” them with the config throughcreateConfig. Alternatively, you can callconnector.setStorageto set things up. We’ll work on a better API and documentation for the latter case in a future version.Hi guys! I think I found the issue in this PR: https://github.com/kenkunz/wagmi-autoconnect-repro/pull/1
let me explain what I see, connectors are constructed with an empty storage property, the function
createConfigtakes connectors as arguments and add them a customized storage which is the one the connector will use to store the shimDisconnect data when you call the connect functionif the connector is not passed through the
createConfigfunction, when callingconnectits storage will be empty, so it won’t save this config on localStorage.Comparing the behavior of the latest
@wagmi/corerelease prior to1.xwith1.xautoConnectshimDisconnectv0.10.11v1.xfalse)true)false)truefalse)falsefalsetrue)falsetruefalsefalsetruetrue)truetruetruefalseI’m having the same issue in a Blitz.js project; on version
0.12.12after our users connected there wallet the connection info would be retained if they left/came back, after upgradingwagmito1.0.2after our users connect their wallet, the info is lost by just refreshing the page.A workaround the OP found (and posted in #2439 comment here) was creating a Local Storage key/val that the old
wagmiversion created which is no longer created in1.x:I looked into ways to not need to manually create this by adding the
shimDisconnectoption to theInjectedConnector, but setting that totrueorfalsedid nothing to fix this.@ombayoeco11 I had the same results as @skplunkerin – setting
shimDisconnecttotrue(orfalse) has no observable impact on theautoConnectbehavior.I checked
localStoragevalues withshimDisconnectset explicitly – thewagmi.injected.shimDisconnectkey is never set.So it appears that:
autoConnectdepends onwagmi.injected.shimDisconnect: true(when usingInjectedConnector)shimDisconnect: trueonInjectedConnectordoes not setwagmi.injected.shimDisconnect: trueSo perhaps the source of the defect is with
InjectedConnectorhandling ofshimDisconnectsetting.