# Storing id returned from database in app\_metadata - unsure whether to do this on the pre or post user registration hook?

**URL:** <https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124>\
**Category:** Get Help\
**Created:** [December 2, 2021, 2:29pm UTC](https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124 "2021-12-02T14:29:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexDring](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexdring/32/33359_2.png) [@AlexDring](https://community.auth0.com/u/AlexDring)\
**Post date:** [December 2, 2021, 2:29pm UTC](https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124/1 "2021-12-02T14:29:23Z")

</div>

I’ve currently setup a post-user registration hook that stores the newly created auth0 user in my database, returning an id from the databse to store in the auth0 app\_metadata:

```auto
const axios = require("axios");

exports.onExecutePostUserRegistration = async (event) => {
  const response = await axios.post("https://mydatabase/api/users/", 
    { 
        auth0_id: event.user.user_id,
        email: event.user.email,
        username: event.user.username, 
    }
  );

  // Store database id in app_metadata
  event.user.app_metadata.db_id = response.data._id
};

```

This seems to do what I want but i’m unsure of whether directly mutating event.user.app\_metadata is the correct way to do this.

I initially setup the post registration hook based on this part of the [Post-User Registration docs.](https://auth0.com/docs/actions/triggers/post-user-registration#store-the-auth0-user-id-in-a-remote-system)

Reading through the [Pre-User Registration](https://auth0.com/docs/hooks/extensibility-points/pre-user-registration) docs however suggests that this might be a better place to add to the app\_metadata:

> At the Pre-User Registration extensibility point, Hooks let you execute custom actions when a new user is created. For example, you can add custom `app_metadata` or `user_metadata` to the newly-created user, or prevent the creation of the user in the database.

Is my current setup using the post-user registration hook ok or should I be doing this in the pre-user registration hook instead?

---

<div class="post-metadata">

**Author:** ![john.gateley](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/john.gateley/32/10879_2.png) [@john.gateley](https://community.auth0.com/u/john.gateley)\
**Post date:** [December 2, 2021, 3:31pm UTC](https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124/2 "2021-12-02T15:31:13Z")

</div>

Hi @AlexDring

The pre-reg hook is preferable, as the post-reg is asynchronous, and you are not guaranteed it will complete before the user’s first login.

John

---

<div class="post-metadata">

**Author:** ![AlexDring](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexdring/32/33359_2.png) [@AlexDring](https://community.auth0.com/u/AlexDring)\
**Post date:** [December 2, 2021, 4:48pm UTC](https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124/3 "2021-12-02T16:48:30Z")

</div>

Hi @john.gateley

Thanks for the quick response. I think I’m going to have to use the post-reg hook as I need the user\_id from the auth0 user and that isn’t available using pre registration.

To avoid having to store a database id in the users meta data, would it be possible to use the auth0 user\_id as an id in the database to reference the user or is that not considered appropriate?

---

<div class="post-metadata">

**Author:** ![john.gateley](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/john.gateley/32/10879_2.png) [@john.gateley](https://community.auth0.com/u/john.gateley)\
**Post date:** [December 3, 2021, 2:03pm UTC](https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124/4 "2021-12-03T14:03:35Z")

</div>

Hi @AlexDring

You can also do it in a rule that checks to see if this is the first time the user has logged in, rather than post-reg.

Both methods you mention (storing your user ID in the Auth0 user’s metadata and storing the Auth0 user ID in your DB) are appropriate, with various trade-offs.

John

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [January 8, 2024, 10:43am UTC](https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124/5 "2024-01-08T10:43:13Z")

</div>

Hey there!

As this topic is related to Rules - Hooks - Actions and Rules & Hooks are being deprecated soon I’m excited to let you know about our next [Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043) on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

[Learn more here!](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043)

---

<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:** [January 22, 2024, 10:44am UTC](https://community.auth0.com/t/storing-id-returned-from-database-in-app-metadata-unsure-whether-to-do-this-on-the-pre-or-post-user-registration-hook/74124/6 "2024-01-22T10:44:00Z")

</div>

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