# Database Configured Multi-Tenancy

**URL:** <https://community.auth0.com/t/database-configured-multi-tenancy/89209>\
**Category:** Get Help\
**Created:** [August 22, 2022, 7:37pm UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209 "2022-08-22T19:37:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew.hartz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/matthew.hartz/32/12776_2.png) [@matthew.hartz](https://community.auth0.com/u/matthew.hartz)\
**Post date:** [August 22, 2022, 7:37pm UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209/1 "2022-08-22T19:37:49Z")

</div>

Hi,

We have multi-tenancy configured at the database level where we have 1 user (stored in auth0) who is configured to multiple tenant along with their roles and permissions stored within our database. Our Auth0 tenant behaves as if there is only 1 tenant (our backend).

We do the roles/permissions validation on our backend.

I was wondering if there is a way to encode the a generated token to include an identifier for which that bearer token will behave on behalf of. Maybe storing the ID as a claim or a scope?

We use dotnet core 5 for this access/bearer token generation.

Thanks!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 23, 2022, 12:30am UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209/3 "2022-08-23T00:30:43Z")

</div>

Hello @matthew.hartz welcome back to the community!

> [@matthew.hartz](#):
>
> I was wondering if there is a way to encode the a generated token to include an identifier for which that bearer token will behave on behalf of. Maybe storing the ID as a claim or a scope?

It sounds like storing the ID as a custom claim might do the trick - Please see the following FAQ:

[https://community.auth0.com/t/adding-custom-claims-to-tokens/84590](https://community.auth0.com/t/adding-custom-claims-to-tokens/84590)

Let us know if you have follow up questions!

---

<div class="post-metadata">

**Author:** ![matthew.hartz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/matthew.hartz/32/12776_2.png) [@matthew.hartz](https://community.auth0.com/u/matthew.hartz)\
**Post date:** [August 23, 2022, 12:39am UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209/4 "2022-08-23T00:39:00Z")

</div>

Hi @ty.frith , is that favorite color passed into the authorize api somehow?

Thanks!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 24, 2022, 1:28am UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209/5 "2022-08-24T01:28:41Z")

</div>

Hey @matthew.hartz! Looking back at the FAQ code, it was missing the reference to the `user_metadata` - It should be something like:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://myapp.example.com';
  const { favorite_color, preferred_contact } = event.user.user_metadata;

  if (event.authorization) {
    // Set claims 
    api.idToken.setCustomClaim(`${namespace}/favorite_color`, favorite_color);
    api.idToken.setCustomClaim(`${namespace}/preferred_contact`, preferred_contact);
  }
};

```

Where `favorite_color` and `preferred_contact` are added to [user\_metadata](https://auth0.com/docs/manage-users/user-accounts/metadata#metadata-types) and can then be referenced via the Action.

---

<div class="post-metadata">

**Author:** ![matthew.hartz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/matthew.hartz/32/12776_2.png) [@matthew.hartz](https://community.auth0.com/u/matthew.hartz)\
**Post date:** [August 24, 2022, 1:57am UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209/6 "2022-08-24T01:57:19Z")

</div>

Hi thanks for the response. What if that does data doesnt exist in the user metadata? I would like to provide it when requesting authorization.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 25, 2022, 9:46pm UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209/7 "2022-08-25T21:46:02Z")

</div>

No problem, happy to help!

> [@matthew.hartz](#):
>
> What if that does data doesnt exist in the user metadata? I would like to provide it when requesting authorization.

Makes sense 🙂 Depending on which library you are using, you can add extra params to the authorize call which can be access in the Action with `event.request.query` (only available in post login action). In [auth0-spa-js](https://github.com/auth0/auth0-spa-js) for example there [auth0clientoptions](https://auth0.github.io/auth0-spa-js/interfaces/auth0clientoptions.html). `event.request.query` is a part of the [event object](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/event-object) associated with Actions.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 9, 2022, 9:46pm UTC](https://community.auth0.com/t/database-configured-multi-tenancy/89209/8 "2022-09-09T21:46:20Z")

</div>

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