Pre-adding metamask users


I am trying to pre-add users into the database so that the system can add points for them into the database. Users would have generated different amount of points by the time they login first time so just setting default points is not in question. The points system is not something that can be calculated at the time of first login.

I pre add the user using this code (so it automatically generates date added etc.)

async function addUser(){
  const user = new Moralis.User();
  user.set("username", "TestUser");
  user.set("password", "TestPass123");
  user.set("email", "[email protected]");
  user.set("accounts", ["0x34ea10572473d6134999cdde77c6a44d16da2926"]) //my metamask test account address
  user.set("ethAddress", "0x34ea10572473d6134999cdde77c6a44d16da2926")
  // other fields can be set just like with Moralis.Object
  try {
    await user.signUp();
    // Hooray! Let them use the app now.
  } catch (error) {
    // Show the error message somewhere and let the user try again.
    alert("Error: " + error.code + " " + error.message);

Then when I try to login with the account that belongs to this ethereum address I get error:
“Uncaught (in promise) Error: Account already exists for this email address.”
And then the login automatically creates a new user into the database without any ethereum address for that metamask account.

I think I would need the code to somehow automatically merge the new account with one that I pre-added when the user logs in for the first time.

If there is a way to pre-add users into the database like this it would be really great for my project.

I would say that you ca solve this problem easier, you save those points in a table, and when user authenticates you can check in a boforeSave hook for example how many points it should have associated. Or you can completely save in a different table those points all the time

1 Like