# Access to to user ID in pre-user signup

**URL:** <https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613>\
**Category:** Get Help\
**Created:** [September 14, 2021, 11:12am UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613 "2021-09-14T11:12:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chinds](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chinds/32/25410_2.png) [@chinds](https://community.auth0.com/u/chinds)\
**Post date:** [September 14, 2021, 11:12am UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/1 "2021-09-14T11:12:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 14, 2021, 4:23pm UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/3 "2021-09-14T16:23:56Z")

</div>

Hi @chinds,

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

> **[Pre User Registration Flow](https://auth0.com/docs/customize/actions/flows-and-triggers/pre-user-registration-flow)**
>
> Learn about the Pre User Registration Flow, which runs when a user attempts to register through a Database or Passwordless connection. It can be used to add metadata to the user profile before it is created or to deny a registration.

---

<div class="post-metadata">

**Author:** ![chinds](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chinds/32/25410_2.png) [@chinds](https://community.auth0.com/u/chinds)\
**Post date:** [September 14, 2021, 4:30pm UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/4 "2021-09-14T16:30:45Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 15, 2021, 3:37pm UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/5 "2021-09-15T15:37:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chinds](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chinds/32/25410_2.png) [@chinds](https://community.auth0.com/u/chinds)\
**Post date:** [September 16, 2021, 8:25am UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/6 "2021-09-16T08:25:13Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 16, 2021, 3:52pm UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/7 "2021-09-16T15:52:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chinds](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chinds/32/25410_2.png) [@chinds](https://community.auth0.com/u/chinds)\
**Post date:** [September 16, 2021, 4:15pm UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/8 "2021-09-16T16:15:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 16, 2021, 4:36pm UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/9 "2021-09-16T16:36:56Z")

</div>

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:

```auto
  if (event.user.app_metadata.org_id) {
    return;
  }

```

---

<div class="post-metadata">

**Author:** ![chinds](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chinds/32/25410_2.png) [@chinds](https://community.auth0.com/u/chinds)\
**Post date:** [September 17, 2021, 11:12am UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/10 "2021-09-17T11:12:13Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 17, 2021, 5:10pm UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/11 "2021-09-17T17:10:19Z")

</div>

> [@chinds](#):
>
> Will the existing user conditional statement have access to the newly updated app\_metadata?

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.

---

<div class="post-metadata">

**Author:** ![chinds](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chinds/32/25410_2.png) [@chinds](https://community.auth0.com/u/chinds)\
**Post date:** [September 21, 2021, 8:11am UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/12 "2021-09-21T08:11:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 6, 2021, 8:11am UTC](https://community.auth0.com/t/access-to-to-user-id-in-pre-user-signup/69613/13 "2021-10-06T08:11:32Z")

</div>

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