aragon / use-wallet

👛 useWallet() · All-in-one solution to connect a dapp to an Ethereum provider.
MIT License
741 stars 240 forks source link

Add auto connect mechanism #70

Open chiku524 opened 3 years ago

chiku524 commented 3 years ago

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 commented 3 years ago

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

0xMeir commented 3 years ago

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.

vortextemporum commented 3 years ago

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

bpierre commented 3 years ago

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.

stoplion commented 3 years ago

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

seniorjoinu commented 3 years ago

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.

philippe-git commented 3 years ago

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,
};
MoukimHF commented 3 years ago

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"); }) }