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

No way to check for a dapp connection without auto-prompting the user to choose a wallet

Open esot321c opened this issue 2 years ago • 3 comments

When a user navigates to a page, we need a way to check if they have an active nautilus connection.

This is problematic because it will automatically prompt the user to choose a wallet if they haven't already connected one:

// @ts-ignore
const injectApi = await ergoConnector.nautilus.connect();
  if (injectApi) {
    // @ts-ignore
    const changeAddress = await ergo.get_change_address();
    if (changeAddress) {
      setDappConnected(true);
      return changeAddress;
  }
}

If we auto-prompt a user, then it negates the "sign in" button. If we decide not to potentially auto-prompt non-logged in users, we have no way of knowing when a user navigates to the page and has already connected in the past.

We can store an active connection in localStorage or a database, but then if the user manually disconnects via Nautilus, the dapp will think they're logged in. It will then use the ergoConnector.nautilus.connect() to instantiate the API, causing an automatic wallet selection prompt. If the app is operating under the premise that the user chose a specific wallet before, and now the user chooses a different one, there are differences between the user's connected wallet and the app session.

The app can compare the two, but then a new session must be created. Sometimes, this forces a page refresh (like with next-auth), and the user-flow is interrupted and not automatically started again.

esot321c avatar Sep 08 '23 00:09 esot321c

Hey, you can use the method ergoConnector.nautilus.isConnected() to check if the dApp is authorized without popping up the connection window.

capt-nemo429 avatar Sep 08 '23 18:09 capt-nemo429

Brilliant! I'll try that today and close the issue.

esot321c avatar Sep 08 '23 20:09 esot321c

const checkDappConnection = async () => {
  const isNautilusConnected = await window.ergoConnector.nautilus.isConnected();
  if (!isNautilusConnected) {
    console.log('Nautilus not connected')
  }
  else console.log('Nautilus is connected')
}
checkDappConnection();

This code always returns "Nautilus not connected"

When changed to const isNautilusConnected = await window.ergoConnector.nautilus.connect(); it works as intended when they are connected, but it prompts a connection when they are not connected (which is not the desired functionality for this app)

esot321c avatar Sep 18 '23 17:09 esot321c

Fixed by #134. Now ergoConnector.nautilus.isConnected() answers correctly

arobsn avatar Jun 24 '24 00:06 arobsn