How to initialize object when a user is created?

I have two things I would like to do when new Users are created, and I’m not sure the best way:

  1. Reverse resolve their ENS name and store it on their User object. Or in other words: how to keep ens names in sync with the Moralis DB?
  2. Initialize other objects in the DB that will be associated with the User.

I was messing with the _beforeSave trigger for Users, but it was having some weird behaviors. And for the ens thing, wasn’t sure if using the Sync feature might be useful?

Did you try to use the resolve reverse API not entirely sure if sync is perfect for your use case as I’m not entirely sure which event to sync with. But if you use our API, you can just directly use it to resolve the address to ENS real time :raised_hands:

what problems did you have with beforeSave for User class?

Okay, so I just tried setting this up again. I’m basically doing:

Moralis.Cloud.beforeSave(Moralis.User, async function (request) {
  if (!request.object.id) {
   const inventory = new Inventory()
   await inventory.save()
   request.object.set('inventory', inventory);
  }
})

And I’m getting the following error message:

Error: {“message”:“Signing message has expired.”,“code”:101}

And the user isn’t created. I assume this is because I’m "await"ing and the authenticate() call needs to happen synchronously.

And to add to this, I want to be able to do:

const user = await Moralis.authenticate()
const inventory = await user.get('inventory').fetch()

So I assume beforeSave would have been the best place for this. I believe my best option now is to create a Cloud function called “fetchInventory” that checks if the user has one and if not, creates it. But I would prefer to explicitly define the initial objects…