Skip to content

Commit

Permalink
Merge pull request #186 from vu3th/feat/185
Browse files Browse the repository at this point in the history
feat(core): Don't auto-connect to MetaMask if it's locked
  • Loading branch information
johnson86tw authored May 9, 2024
2 parents c9f62f7 + 10eaa44 commit ebf773a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ const { darkMode } = storeToRefs(useAppStore())

<NuxtPage />

<VueDappModal :dark="darkMode" :hideConnectingModal="hideConnectingModal" />
<VueDappModal
:dark="darkMode"
auto-connect
auto-connect-browser-wallet-if-solo
:hideConnectingModal="hideConnectingModal"
/>
</NuxtLayout>
</n-config-provider>
</template>
24 changes: 23 additions & 1 deletion packages/core/src/useAutoConnect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { onMounted, ref } from 'vue'
import { useConnect } from './services/connect'
import { ConnectOptions, ProviderTarget } from './types'
import { ConnectOptions, ProviderTarget, RdnsEnum } from './types'
import { useConnectors } from './services/connectors'
import { getLastConnectedBrowserWallet } from './services/localStorage'
import { isWindowEthereumAvailable } from './utils'
import { useEIP6963 } from './services/eip6963'

export function useAutoConnect(pinia?: any) {
const isAutoConnecting = ref(false)
Expand Down Expand Up @@ -44,6 +45,27 @@ export function useAutoConnect(pinia?: any) {
const lastRdns = getLastConnectedBrowserWallet()
if (!lastRdns) return

/**
* feat: Don't auto-connect to MetaMask if it's locked
* issue#185: https://github.com/vu3th/vue-dapp/issues/185
*/

// if the wallet is MetaMask, check if it's unlocked
if (lastRdns === RdnsEnum.metamask) {
const { providerDetails } = useEIP6963()
const providerDetail = providerDetails.value.find(p => p.info.rdns === RdnsEnum.metamask)
if (providerDetail) {
const provider = providerDetail.provider
/**
* isUnlocked - API Docs: https://docs.metamask.io/wallet/reference/provider-api/#_metamaskisunlocked
* How to check if a web3 wallet is unlocked? - Stack Overflow: https://stackoverflow.com/questions/69015014/how-to-check-if-a-web3-wallet-is-unlocked
*/
// @ts-ignore
const isUnlocked = await provider._metamask.isUnlocked()
if (!isUnlocked) return
}
}

options = { target: 'rdns', rdns: lastRdns }
break
default:
Expand Down

0 comments on commit ebf773a

Please sign in to comment.