Pre User Registration adding of 'user_metadata'

Hi,
I am trying to add ‘user_metadata’ for each user on registration.
I am currently allowing users to only register via Spotify. It is all working fine for me, except that i can’t create a default value in my ‘user_metadata’.
I have followed the example for it, but cannot get it to work.
Do i need to pass values into it?
exports.onExecutePreUserRegistration = async (event, api) => { /**

  • Handler that will be called during the execution of a PreUserRegistration flow.
  • @param {Event} event - Details about the context and user that is attempting to register.
  • @param {PreUserRegistrationAPI} api - Interface whose methods can be used to change the behavior of the signup.
    */

module.exports = function (user, context, cb) {
var response = {};
// Add user or app metadata to the newly-created user
response.user = {
user_metadata: { foo: ‘bar’ },
app_metadata: { vip: true, score: 7 }
};
response.user = user;
cb(null, response);
};
};

Hi @killianoneachtain,

According to the docs, the pre-user registration action trigger only runs for Database and Passwordless users, not for social connection users such as Spotify.

You can instead use a Post Login Action and check if it is the first login:

exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count === 1) {
    api.user.setAppMetadata('vip', true);
  }
};
1 Like

Thank you so much for that. I had tried to use Post-Registration Actions, but that wasn’t working either.
It is working as hoped/expected now.
Thanks again.

1 Like