WalletConnect / web3modal-react-native

Web3Modal React-Native SDK
https://web3modal.com
Apache License 2.0
51 stars 16 forks source link

Question: how to reuse an existing session? #17

Closed alsakhaev closed 1 year ago

alsakhaev commented 1 year ago

After app reloading I have to connect a wallet again. The provider property from the hook is null until I call the open() function. After calling (the modal is shown, but I didn't select a wallet yet) I see that provider.session exists and there is info about my wallet, but isConnected is still false. I'd like to understand how to reuse the previous connection to the wallet.

ignaciosantise commented 1 year ago

Hey @alsakhaev, we've just created a PR to reuse the existing session. After that merge, the session will load automatically

ignaciosantise commented 1 year ago

@alsakhaev we released a new alpha version that reuses the existing session automatically 💪 Closing the issue.

taibenvenuti commented 1 year ago

This still doesn't work for me. provider is undefined until the open function is called.

ignaciosantise commented 1 year ago

Hey @taibenvenuti 👋 are you using the last version of the SDK? Can you upload the piece of code where you render the Web3Modal component?

taibenvenuti commented 1 year ago

Yes I updated to alpha3

import './expo-crypto-shim.js';
import '@walletconnect/react-native-compat';
import '@ethersproject/shims';
import { ThemeProvider } from './src/styles/ThemeContext';
import { Navigation } from './src/navigation/Navigation';
import { Provider } from 'react-redux';
import { persistor, store } from './src/store/store';
import { PersistGate } from 'redux-persist/integration/react';
import { AuthenticationProvider } from './src/services/AuthenticationContext';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { Web3Modal } from '@web3modal/react-native';
import { PROJECT_ID } from '@env';
import { AssetsProvider } from './src/services/AssetsContext';
import { providerMetadata } from './src/Config';

export default function App() {
  return (
    <Provider store={store}>
      <Web3Modal projectId={PROJECT_ID} providerMetadata={providerMetadata} />
      <PersistGate loading={null} persistor={persistor}>
        <ThemeProvider>
          <AuthenticationProvider>
            <AssetsProvider>
              <SafeAreaProvider style={{ margin: 0 }}>
                <Navigation />
              </SafeAreaProvider>
            </AssetsProvider>
          </AuthenticationProvider>
        </ThemeProvider>
      </PersistGate>
    </Provider>
  );
}
ignaciosantise commented 1 year ago

@taibenvenuti When <Web3Modal /> renders, it initializes the provider and automatically restores the connection with the wallet (if exists).

If you didn't connect with a wallet before, when you open the modal a new session is created but it wont be saved unless you actually connect with a wallet.

ozgurrgul commented 3 weeks ago

Hi @ignaciosantise question: do we have option to not to store/persist the session or our only option is to disconnect manually visa provider?.disconnect() ? We don't want to persist isConnected/address or other variables on the app startup.

We are initializing the modal with following versions and method:

"@walletconnect/modal-react-native": "^1.1.0",
"@walletconnect/react-native-compat": "^2.15.0",
export const ProviderMetadata: IProviderMetadata = {
  name: 'Brand',
  description: 'Connect your wallet',
  url: 'https://your-project-website.com/',
  icons: ['https://your-project-logo.com/'],
  redirect: {
    native: `namespace://`
  }
};

// 
const { open, isConnected, provider, address } = useWalletConnectModal();
//
<WalletConnectModal projectId={projectId} providerMetadata={ProviderMetadata} />
ignaciosantise commented 3 weeks ago

hey @ozgurrgul 👋 the SDK restores the active session automatically. So if you want to remove a session you need to call disconnect, as it will also notify the wallet to disconnect on their side too.

Cleaning values without calling disconnect might leave the connection open, so i recommend not doing that