MetaMask / metamask-docs

Developer documentation for MetaMask
https://docs.metamask.io
Apache License 2.0
653 stars 936 forks source link

cannot connect to website on second account [solved] #226

Closed C4rlosM4tos closed 3 years ago

C4rlosM4tos commented 3 years ago

if i request access to my website on the first account, the metamask popup comes up as expected, but when i change the account, then the second account cannot request access anymore until i removed the first account or the user (me) will need to connect manually. am i doing something wrong?

utkarshg6 commented 3 years ago

Hi, @C4rlosM4tos,

You may create a check whether or not the account is received through this request.

const accounts = await ethereum.request({ method: 'eth_accounts' })

Then, you can request the new account (the changed MetaMask account) using this request.

const accounts = await ethereum.request({ method: 'eth_requestAccounts' });

In case you prefer an alternative method, please refer to this repository simple-dapp-tutorial or this part of the documentation Create a Simple Dapp.

C4rlosM4tos commented 3 years ago

i use ethereum.request, the popup comes up and the user has to accept to connect the account to the site. but when i change account in metamask,, and hit the button that runs the request (or the event account changed), no popup comes up anymore, and the only way to connect the second account is to connect the site manually in metamask. after that, it read the accounts as expected when i change them again. only when i disconnect all connected accounts the metamask popup is shown to request access again for the selected account... async function handleEthButtonClick (){ await window.ethereum.request({ method: 'eth_requestAccounts' }) .then((account) => {
setAccounts(account[0]) console.log("connect to metamask: " + account[0]) setMessage("connected account: " + account[0])

its like window.selectedAddress won't change to new real selected address if the website has not been approved yet.

BboyAkers commented 3 years ago

Have you looked at this page? https://docs.metamask.io/guide/accessing-accounts.html

ethereum.on('accountsChanged', function (accounts) {
  // Time to reload your interface with accounts[0]!
});
C4rlosM4tos commented 3 years ago

hi,

yes, and it does the same thing as my problem...

what happens if you do it?

the website you linked... click the button it shows runs the popup and show the address right?

now change account to a second account....

i can't even hit the button on that site no more, no way to get a popup to request access for the second account, and the website it not showing the right address anymore, but the old one you clicked for on stage 1, but not the second address 🙁

thank you for looking into it

Greetz

edit: also in console (on the docs site) when i run ethereum.selectedAddress its not showing the right one (after change account) because it did not get approval...


Van: Austin Akers notifications@github.com Verzonden: maandag 22 februari 2021 19:09 Aan: MetaMask/metamask-docs metamask-docs@noreply.github.com CC: C4rlosM4tos treve01@hotmail.com; Mention mention@noreply.github.com Onderwerp: Re: [MetaMask/metamask-docs] cannot connect to website on second account (#226)

Have you looked on this page? https://docs.metamask.io/guide/accessing-accounts.html

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/MetaMask/metamask-docs/issues/226#issuecomment-783565678, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AE6ADLCRIQ352V5QTDHKKNLTAKMXNANCNFSM4X6HZ24A.

C4rlosM4tos commented 3 years ago

the event account changed only works when the account has already been approved, but there is no way for that second account to request approval :s

BboyAkers commented 3 years ago

Hmm odd. I was able to request approval and change to my second account. Going between both back and forth works. You'll always need approval on accounts/addresses that haven't been connected to a Dapp before when initially connecting and changing address for security purposes. Afterwards you SHOULD have no problem. Is there a repo I can take a look at for your website and could you show a video of what you're doing? 🙂

C4rlosM4tos commented 3 years ago

weird, my metamask should be running the latest version, i don't see why it should behave different for you than for me.

can you confirmed you had the metamask popup for the second account as well?

maybe its just a problem with my metamask and not the code??

i'm a bit confused now.

greetz


Van: Austin Akers notifications@github.com Verzonden: maandag 22 februari 2021 19:56 Aan: MetaMask/metamask-docs metamask-docs@noreply.github.com CC: C4rlosM4tos treve01@hotmail.com; Mention mention@noreply.github.com Onderwerp: Re: [MetaMask/metamask-docs] cannot connect to website on second account (#226)

Hmm odd. I was able to request approval and change to my second account. Going between both back and forth works. You'll always need approval on accounts/addresses that haven't been connected to a Dapp before when initially connecting and changing address for security purposes. Afterwards you SHOULD have no problem. Is there a repo I can take a look at for your website and could you show a video of what you're doing? 🙂

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/MetaMask/metamask-docs/issues/226#issuecomment-783596088, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AE6ADLBKKWD7P4HQXRSSHF3TAKSEFANCNFSM4X6HZ24A.

C4rlosM4tos commented 3 years ago

i just tried again on chrome instead of firefox this time, but its still acting the same. maybe it is because you give it access to the second account on the first request already? but if i just give it access to the first account, then go back to the site, then change account to second account, it won't detect account changed, and it will not open the popup to request access, as long as the first account is still connected. removing the connection from the first accounts leasd to a popup for the second account. at least this are my findings after testing a bit

BboyAkers commented 3 years ago

It's okay! Hmmm, check this out in the docs on how account change works 🙂 .

Change Account Component: https://github.com/MetaMask/metamask-docs/blob/master/docs/.vuepress/components/ChangeAccount.vue

Also, the Test-Dapp: https://github.com/MetaMask/test-dapp/blob/f85e5af02a1fed8bddde10784500e39bbec7bd55/src/index.js#L869

If you can create a short clip of you going through the flow that would help tremendously.

C4rlosM4tos commented 3 years ago

i'm not really a dev or anything, i hardly know what i am doing.

i guess its just how metamask works for now, i changed account on uniswap and it also did not detect the account change, or asked for access. same for the doc file example button.

its also doing it on 2 different browsers.

have a good night.

grtz


Van: Austin Akers notifications@github.com Verzonden: maandag 22 februari 2021 21:39 Aan: MetaMask/metamask-docs metamask-docs@noreply.github.com CC: C4rlosM4tos treve01@hotmail.com; Mention mention@noreply.github.com Onderwerp: Re: [MetaMask/metamask-docs] cannot connect to website on second account (#226)

It's okay! Hmmm, check this out in the docs on how account change works 🙂 .

Change Account Component: https://github.com/MetaMask/metamask-docs/blob/master/docs/.vuepress/components/ChangeAccount.vue

Also, the test-dapp: https://github.com/MetaMask/test-dapp/blob/f85e5af02a1fed8bddde10784500e39bbec7bd55/src/index.js#L869

If you can create a short clip of you going through the flow that would help tremendously.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/MetaMask/metamask-docs/issues/226#issuecomment-783659965, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AE6ADLFKJQYQLOLC6TM7J5LTAK6JDANCNFSM4X6HZ24A.

C4rlosM4tos commented 3 years ago

https://metamask.github.io/test-dapp/

1: hit connect button with first account. 2: read eth accounts with eth_account button. 3: go to metamask plugin and change account (to one that not has been granted access before) 4: try to click the connect button (on the dapps website), it doesn't do anything anymore, hence the account cannot connect to the dapp 5: click eth_accounts button again: it still reads the old accounts address and not the new address that is selected in metamask during step 3. so it seems the doc, uniswap and this dapp all doing the same and cannot connect to the second account without the user connecting it manual

C4rlosM4tos commented 3 years ago

i think i need ethereum.request({ method: 'wallet_requestPermissions', params: [{ eth_accounts: {} }], }) and confused it with 'ethereum.request({ method: 'request_accounts' }).

anywhere i can learn about what methods are available and what they do? sorry for wasting time and bandwidth

C4rlosM4tos commented 3 years ago

i guess i was using the things wrong, wallet_requestPermissions is what i actually was looking for, it even nicely disconnects the other accounts as well. thank you for making me look at the dapp better

BboyAkers commented 3 years ago

Just looked, glad it was solved 🙂 please reach out if you ever have any more questions!! Closing this issue

BboyAkers commented 3 years ago

Oh and as a side note. I'll make sure to add the list of methods available for use in ethereum.request() 🙂 . Thanks for pointing that out! I'll try to get started with that tonight in the docs!

C4rlosM4tos commented 3 years ago

thank you all for helping. maybe 1 more not so smart question... can i use ethereum.request(methode:) to interact with smart contracts and their functions, or i need to use web3 lib for that? i got a bit confused

BboyAkers commented 3 years ago

Here's the ethereum.request() method list

I also found it in the docs for future reference as well! 🙂(check the first sentence in that section) https://docs.metamask.io/guide/rpc-api.html#ethereum-json-rpc-methods It seems that the docs might need to make that wiki list a bit more obvious to look at. It took me a moment to find it 😂

You'll need web3 or ethers.js 🙂 to interact with contracts. I know that MetaMask is using ethers.js instead of web3.js now. I think it's because it's a much smaller package.