Link user with wallet

Hi everyone,
I am trying to understand how to link a wallet to a user created with an email/username. So, I want the user to sign up / log in, with their username/email and then provide them with the metamask login. How can I now link the user with the wallet? Or is it not supposed to be that way?

Check the docs for β€œlink” and try it

It’s possible to do either. You can first authenticate with username/password then link a Web3 address. Or sign up with Web3 then add a username/password afterward. I would recommend signing up with user/password first, especially if you want to use email as the username as the signUp() function will explicitly make sure the email is unique. It’s possible to run into trouble if you just set the email on the user object then save it.

https://docs.moralis.io/moralis-sdk/users#sign-up-with-username

https://docs.moralis.io/moralis-sdk/crypto-login#linking-addresses

2 Likes

UPDATE:
Got it to work. Here is the code snippet for future readers:

const checkIfLinked = async () => {
    const currentUser = Moralis.User.current();
    const accountLinked = currentUser.attributes.accounts.includes(
      window.ethereum.selectedAddress
    );
    return accountLinked;
  };

  const connectUserWallet = async () => {
    const isLinked = await checkIfLinked();
    console.log(isLinked);
    if (Moralis.Web3) {
      if (isLinked === true) {
        console.log(isLinked);
        await Moralis.Web3.enable();
      }
      if (isLinked === false) {
        if (
          window.confirm(
            "Would you like to link this account to your user profile?"
          )
        ) {
          await Moralis.Web3.link(window.ethereum.selectedAddress);
        }
      }
    }
  };
ProblemDescription

Hi guys,
thanks for the response. Finally found some time to try this.
I could link one address to the currentUser. But when trying to link a second address it gives me:

RESTController.js:304 POST https://gqy0xtb7akau.moralis.io:2053/server/classes/_User/bJbNUnsXRtATxyGoKike691q 400

The second address is also not added in the database.

Here is my try:

codeLinkAddressToUser
function ConnectWallet({ getCurrentUser }) {
  const connectUserWallet = async () => {
    const currentUser = Moralis.User.current();
    const accountLinked = currentUser.attributes.accounts.includes(
      window.ethereum.selectedAddress
    );

    if (Moralis.Web3) {
      if (accountLinked) {
        console.log(accountLinked);
        await Moralis.Web3.enable();
      }
      if (!accountLinked) {
        if (
          window.confirm(
            "Would you like to link this account to your user profile?"
          )
        ) {
          await Moralis.Web3.link(window.ethereum.selectedAddress);
        }
      }
    }
  };

Any clues?

2 Likes

UPDATE:
Ok I got it guys. I did get the error because the address I wanted to link was already linked to another user. And in that case it actually should throw the access error!! Great job Moralis. I start to like your SDK more and more!

ProblemDescription

As stated in the previous post, I got the linking work. But now running into the same issue when the user tryies to link another address during the same session. Again this error:

RESTController.js:304 POST https://gqy0xtb7akau.moralis.io:2053/server/classes/_User/bJbNUnsXRtATxyGoKike691q 400

So if the user connects a new address, it will link successfully the first time during the current session. But if the user wants to link another address during the current session, it throws.
Any clues?

2 Likes