use-wallet icon indicating copy to clipboard operation
use-wallet copied to clipboard

Add auto connect mechanism

Open chiku524 opened this issue 5 years ago • 9 comments

So I am able to connect the wallet successfully, but if I happen to refresh the page, the wallet will disconnect and I need to reconnect it again.

Wouldn't it make sense for the wallet to only disconnect when using the wallet.reset() function?

chiku524 avatar Nov 10 '20 21:11 chiku524

This is for the 'fortmatic' and 'portis' connectors, by the way. Haven't tested the other ones out yet

chiku524 avatar Nov 10 '20 21:11 chiku524

Same happens with meta mask. Would be great if we can connect automatically after user connects their wallet for the first time. This should be possible, on sites like Uniswap they do this.

0xMeir avatar Nov 26 '20 03:11 0xMeir

Unfortunately I also encountered the same problem. It is the only reason I am not using this component for my project at the moment.

vortextemporum avatar Dec 05 '20 15:12 vortextemporum

Agreed, it would be a great feature to have! Feel free to open a PR with it, otherwise I’ll have a look after https://github.com/aragon/use-wallet/pull/51 gets merged.

bpierre avatar Dec 05 '20 15:12 bpierre

Has anyone found a way around this? I just added useWallet to my app, running into same issue

stoplion avatar Jan 05 '21 08:01 stoplion

I just made a couple of functions like

export function setWalletConnected(value: keyof Connectors | null) {
  localStorage.setItem('__WALLET_CONNECTED', JSON.stringify(value));
}

export function isWalletConnected(): keyof Connectors | null {
  const val = localStorage.getItem('__WALLET_CONNECTED');

  return val ? JSON.parse(val) : null;
}

When a user connects their wallet, I call setWalletConnected('injected'). Then, when the app is loaded, I just do this

  const connectedId = isWalletConnected();
  const wallet = useWallet();
  if (wallet.status == 'disconnected' && connectedId != null) {
    wallet.connect(connectedId)
  }

I don't know if it works for other connectors, but Metamask's one works fine.

seniorjoinu avatar Jan 25 '21 21:01 seniorjoinu

Another more solid approach is available in the web3-react repo: the useEagerConnect hook. The code for that hook looks like this once adapted to use-wallet by using the web3-react instance exposed by useWallet:

import { useState, useEffect } from 'react';
import { useWallet } from 'use-wallet';

const useEagerConnect = () => {
  const { _web3ReactContext, connectors } = useWallet();
  const { activate, active } = _web3ReactContext;
  const injected = connectors.injected.web3ReactConnector({ chainId: CHAIN_ID });
  const [tried, setTried] = useState(false);

  useEffect(() => {
    injected.isAuthorized().then((isAuthorized) => {
      if (isAuthorized) {
        activate(injected, undefined, true).catch(() => {
          setTried(true);
        });
      } else {
        setTried(true);
      }
    });
  }, []); // intentionally only running on mount (make sure it's only mounted once :))

  // if the connection worked, wait until we get confirmation of that to flip the flag
  useEffect(() => {
    if (!tried && active) {
      setTried(true);
    }
  }, [tried, active]);

  return tried;
};

export {
  useEagerConnect,
};

philippe-git avatar Apr 16 '21 15:04 philippe-git

This works fine for me

const { account, connect, reset, status } = useWallet(); React.useEffect(() => { if(localStorage.getItem("status")==="connected"){ connect("injected") } },[]);

const resetCb = () => { reset(); localStorage.setItem("status", "disconnected"); };

const connectCb = () => { connect("injected").then(() => { localStorage.setItem("status", "connected"); }) }

MoukimHF avatar Aug 04 '21 06:08 MoukimHF