Access to to user ID in pre-user signup

I need to store some info in the users app_metadata before their first login (The data is required for another login action). These details are determined by hitting an api with the user’s ID, data is then returned and my plan was to store it in the users app_metadata using the pre-user signup action.

However, It doesn’t look like the user ID is available in this action?

I can try using the post-user signup action but my concerns are;

a) Will the user have signed in before this is run not giving it enough time to update the user app metadata?

b) There is no api event on this action so not sure if updating the app_metadata is possible from here.

Hi @chinds,

It looks like there is an example in the doc doing exactly what you are requesting. Have you seen this?

That’s the example I am following, however according to the API docs here, the user object on the event does not have the user_id

The user ID (remoteUser.id) in this scenario is coming from a remote DB. It is an external user ID that is being stored in auth0 in addition to the user ID that is created by Auth0.

Can you post the code (or some pseudocode) for your Action? I’d like to have a better understanding of what the rule does.

Ok so here is some very basic pseudocode for what I want to happen during the signup process.

function (event, api) => {
    // get Auth0 user ID

    // Post to external API, create an organisation with Auth0 user id as the owner ID
    const organisation = await createOrgApi(auth0_user_id);

    // Update users app_metadata
    updateAppMetadata(organisation.id)
}

Once the user is signed up, I have another action that is part of the login flow, and will use the oprganisation ID on the users app_metadata.

Could you to handle this in a post-login action (in this type of action the user’s ID will be available and you can update the user’s metadata)? If you run it as the first action in the post-login pipeline, the data should be available for the subsequent actions.

This could work, I would just need to ensure that I add some checks before hitting my external API to ensure I do not create the new organisation with every login and to ensure that the login flow isn’t slowed down too much.

I’ll give this a shot, Thanks

One strategy for this would be to check for an organization ID in the users app metadata before running the rest of the action. If the parameters exists you can skip the action.

Like this:

  if (event.user.app_metadata.org_id) {
    return;
  }
1 Like

Just thinking about this a little more, once the app_metadata is updated will the rest of the action have access to the updated metadata?

basic implementation of my login action

const login = (event, api) => {
    const { user: { app_metadata } } = event;

    // New user, fetch from external api and update app_metadata
    if (!app_metdata.organisation) {
        const data = await fetch("url")

        api.user.setAppMetaData("organisation", { id: data.id })
    }

    // Existing user
    if (app_metdata.organisation) {
        // extra code to generate access token and set custom claim with org id
    }
}

Will the existing user conditional statement have access to the newly updated app_metadata? If not I can just run the extra code I need in the first conditional so not a problem.

No, the data you just set won’t be available in event.user.app_metadata in this action.

You can create another post-login action that runs after this one and the metadata will be available in that one.

I just tested this to confirm.

Thats a great idea, That helps clean up and separate the code nicely. This is working well. Thanks for your help.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.