# Storing non-auth user data outside of auth0, custom user\_id in tokens and multiple id providers for the same user

**URL:** <https://community.auth0.com/t/storing-non-auth-user-data-outside-of-auth0-custom-user-id-in-tokens-and-multiple-id-providers-for-the-same-user/21320>\
**Category:** Get Help\
**Tags:** app\_metadata, jwt, custom-database\
**Created:** [February 14, 2019, 3:55pm UTC](https://community.auth0.com/t/storing-non-auth-user-data-outside-of-auth0-custom-user-id-in-tokens-and-multiple-id-providers-for-the-same-user/21320 "2019-02-14T15:55:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brodge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/brodge/32/14676_2.png) [@brodge](https://community.auth0.com/u/brodge)\
**Post date:** [February 14, 2019, 3:55pm UTC](https://community.auth0.com/t/storing-non-auth-user-data-outside-of-auth0-custom-user-id-in-tokens-and-multiple-id-providers-for-the-same-user/21320/1 "2019-02-14T15:55:10Z")

</div>

Hi there,

I’m currently implementing Auth0 to provide auth to many microservices. Including a ‘UserService’ that holds non-auth related data about each user.

Some of the other microservices need a user identifier to apply authorization - I don’t want to use the Auth0 id for this because I don’t want to rule out authenticating via other methods in the future, also it’s my understanding that the same user could authenticate with Lock via Google / another identity provider and end up with a differed ‘sub’ claim in their token. Ideally I want users to be able to login through Auth0 via any identity provider and still access the same user account on our side (if they choose to).

It would be great if we could put our own user\_id inside the JWT access\_token issued by Auth0 so I don’t need to be always calling the UserService to look it up when the JWT is validated.

On the [User Data Storages docs](https://auth0.com/docs/best-practices/user-data-storage-best-practices) it seems to recommend using a ‘custom database’ solution, but on [this page](https://auth0.com/docs/connections/database/custom-db) it seems to define that approach as a ‘legacy’ approach (and you need enterprise?)

I’ve read that this can be achieved via ‘post-registration’ hooks, so every-time a user registers via Lock, it creates that user in our UserService (via HTTP POST?), then attaches the user\_id returned to the app\_metadata for inclusion in future access\_tokens…but what if they login via Google / Facebook where a ‘registration’ doesn’t really happen?

Would this also work with ‘pre-registration’ hooks? I’m concerned that if user registers as part of a flow, the asynchronous nature of the ‘post-reg’ hook might not have been completed by the time they are redirected to the service that requires the user\_id to be present in the JWT.

What’s the recommended way to achieve this?

Sorry for all the questions! Thanks!

---

<div class="post-metadata">

**Author:** ![nicolas\_sabena](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nicolas_sabena/32/6064_2.png) [@nicolas\_sabena](https://community.auth0.com/u/nicolas_sabena)\
**Post date:** [February 16, 2019, 3:13pm UTC](https://community.auth0.com/t/storing-non-auth-user-data-outside-of-auth0-custom-user-id-in-tokens-and-multiple-id-providers-for-the-same-user/21320/2 "2019-02-16T15:13:01Z")

</div>

Hi @brodge

The documents you link to talk about two different types of “custom database” concepts:

- One is the custom database for authentication purposes. This is when your user authentication data (user id and password hash) is stored on an external database (because that’s the system you’ve been using before, and thus “legacy”). For this scenario, we provide the “Custom DB” connection type.

- The other reference to “custom database” is talking about your application data, i.e. data outside of the Auth0 context. For instance, if you were building a calendar app, calendar entries and user settings related to the calendar app should probably go in the “custom database” for the calendar app, not in Auth0.

Pre and post registration hooks only run for database connections, not for social connections (because the registration happened before, at the social identity provider).  
But you can create a rule that checks for this alternative user id on each login, and assigns that id if it’s not present. E.g.:

```auto
function (user, context, callback) {
  
  function addCustomClaim() {
    // add internal id in the ID Token
    context.idToken["https://yourapp.com/claims/internal_id"] = user.app_metadata.internal_id;
  }

  user.app_metadata = user.app_metadata || {};
  if (!user.app_metadata.internal_id) {
    var newId = generateNewId(); // you'll need to implement this
    user.app_metadata.internal_id = newId;
    // persist the changes
    auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
      .then(function(){
        addCustomClaim();
        callback(null, user, context);
      })
      .catch(function(err){
        callback(err);
      });
  } else {
    addCustomClaim();
    callback(null, user, context);
  }

```

That’ll give you an internal id for each user (whether that’s useful or not is another discussion). But it will also give you different internal ids when the user logs in with a different identity provider (because the user profile in Auth0 is different).  
If you want the same user ID for cases where the email address is the same (or when the user can prove that the identity is equivalent by any other method) you might want to check [Account linking](https://auth0.com/docs/link-accounts), where you link two or more Auth0 identities so that regardless of the authentication method used by the user, the application sees the same identity.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![brodge](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/brodge/32/14676_2.png) [@brodge](https://community.auth0.com/u/brodge)\
**Post date:** [February 19, 2019, 2:09pm UTC](https://community.auth0.com/t/storing-non-auth-user-data-outside-of-auth0-custom-user-id-in-tokens-and-multiple-id-providers-for-the-same-user/21320/3 "2019-02-19T14:09:21Z")

</div>

Very helpful, thanks very much!

---

<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:** [August 29, 2019, 11:27am UTC](https://community.auth0.com/t/storing-non-auth-user-data-outside-of-auth0-custom-user-id-in-tokens-and-multiple-id-providers-for-the-same-user/21320/4 "2019-08-29T11:27:00Z")

</div>

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