[SOLVED] Link metamask/walletconnect after a user is already logged in with username+password

Dapp:

Users can register and sign in
Using if (!isAuthenticated) {} to ask for the login info before rendering the app

Problem:

After a successful log-in with username+password I try to call the authenticate(); to connect to metamask, but it seems that it logs out of the account and then asks to connect using metamask.

Expected behavior: after user is already logged in, click connect using metamask and add that account to the user on the database.

Possible solutions:

Use await Moralis.link(accounts[0]);, I tried to use this but because you are not connected there is no way get the accounts out of the window metamask

Help would be greatly appreciated :’)

If you try Moralis.link() what it does in your case?

Or this: I try to link address (new) to existing username, password login

I got:
Unhandled Runtime Error TypeError: Moralis.Link is not a function

Let me take a look to the link, I will report back in a few min

Used:

        x = await web3.eth.getAccounts();
        console.log(x)

but got:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getAccounts')
I am using "moralis": "^0.0.97"

ok, you may need to enable web3 before

web3 = await Moralis.enableWeb3()
x = await web3.eth.getAccounts()
1 Like

Got it working using this:

        let currentAddress = await window.ethereum.send("eth_requestAccounts");
        await Moralis.link(currentAddress.result[0], { signingMessage: "Link Wallet To Account" });

image

image

Your solution also works:

        let web3 = await Moralis.enableWeb3()
        let x = await web3.eth.getAccounts()
        let y = x[0]
        console.log(y)
        await Moralis.link(y, { signingMessage: "Link Wallet To Account" });

Thanks !

2 Likes

I’m having trouble here, it used to work in the past using web3, now using Moralis 1.3.1 and trying to Link a new wallet to a User that has logged in with user/pass:

    const addWallet = async () => {
      const web3 = await Moralis.enableWeb3()
      const currentAccount = web3.provider.selectedAddress
      try {
        await Moralis.link(currentAccount, {signingMessage: `Link this wallet to your ${appName.value} account`})
      } catch (err) {
        console.log(err);
      }

It get:

Same result when trying to link a new wallet to a user that is already logged in with a wallet.
CLP all set Public on User and _EthAddress.

Hard to debug the error message.

you get the right address here, and after that you get the metamask popup where it asks you to sign a message?

yes! any idea how i can provide more debug data?

by the way, i get the same doing it like this:

      Moralis.onAccountChanged(async (accounts) => {
        const user = Moralis.User.current()
        if (!user || !user.attributes.accounts) return  // not logged in with a connected wallet
        try {
          const address = accounts[0];
          if (addressAlreadyLinked(user, address)) {
            console.log('address already linked');
            return;
          }
          const confirmed = confirm("Link this address to your account?");
          if (confirmed) {
            await Moralis.link(address);
            alert("Address added!");
          }

what is the message that you see that it pops up for signing in metamask?

in the last code (where I dont set a custom mssg):

ok, and when you set a custom message? (it will not work with that message)

yeah same:

ok, I think that it is a problem in the SDK of not adding extra info to that message

i can still work around it for now, let me know if you need anything

yeah i see now it’s missing the ID in the message

yes, it looks like this line is not present in link: https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/760975e72b9fdf3451f5415093afccf1fb00ec9d/src/MoralisDot.js#L28

Can you try again now with latest sdk version?

works perfectly in Moralis v1.3.2 :pray:

1 Like