Is it possible to assign a custom claim to a JWT token on signup using flows?

Whenever a user signs up to our Auth0 infrastructure, I’d like to assign him a unique mycompany_id and then include that id in access tokens (added a custom action to the login flow to add the id to the token, which is working fine).

However, it seems like this doesn’t work on signup. If I use a pre-registration flow, request the mycompany_id from our API and then set that id as user_metadata it isn’t saved in the database. If I use a post-registration flow this works fine, however the id is not included in the first access token after the signup process, but only in subsequent logins. Is there a way to achieve this with the Action flows? Or do I need to use Hooks/Rules instead?

This is my Action I’m currently using for testing:

exports.onExecutePreUserRegistration = async (event, api) => {

  const axios = require('axios')

  axios
    .post('https://mycompanyapi/v1/users', {}, {
      headers: {
        "X-API-KEY": event.secrets.API_KEY
      }
    })
    .then(res => {
      if (res.statusCode == 201) {
        api.user.setUserMetadata("mycopmany_id", res.data.mycopmany_id)
      } else {
        console.error("invalid status code from create user response")
        console.error(res)
        api.access.deny("failed to create mycompany_id", "internal server error")
      }
    })
    .catch(error => {
      console.error("failed to create a new user")
      console.error(error)
      api.access.deny("failed to create mycompany_id", "internal server error")
    })
};

Forgot to return the axios call, now it’s working

1 Like

Perfect! Thanks for sharing it with the rest of community!