# How to handle multiple api's and access tokens with express-openid-connect

**URL:** <https://community.auth0.com/t/how-to-handle-multiple-apis-and-access-tokens-with-express-openid-connect/99025>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [January 26, 2023, 2:36pm UTC](https://community.auth0.com/t/how-to-handle-multiple-apis-and-access-tokens-with-express-openid-connect/99025 "2023-01-26T14:36:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhautala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mhautala/32/33331_2.png) [@mhautala](https://community.auth0.com/u/mhautala)\
**Post date:** [January 26, 2023, 2:36pm UTC](https://community.auth0.com/t/how-to-handle-multiple-apis-and-access-tokens-with-express-openid-connect/99025/1 "2023-01-26T14:36:11Z")

</div>

If you have two separate API’s because they have a different security context and hence one api has a longer lifetime and the other a shorter one.

How would you implement handling the two different access tokens required in the application side with the Auth0’s express openid connect library [GitHub - auth0/express-openid-connect: An Express.js middleware to protect OpenID Connect web applications.](https://github.com/auth0/express-openid-connect)?

---

<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:** [January 26, 2023, 10:50pm UTC](https://community.auth0.com/t/how-to-handle-multiple-apis-and-access-tokens-with-express-openid-connect/99025/2 "2023-01-26T22:50:01Z")

</div>

Hey there @mhautala great question!

While we typically recommend a single [logical API](https://auth0.com/docs/get-started/apis/set-logical-api) approach, this doesn’t exactly make sense for your use case.

I haven’t tried this out myself but I believe our SDKs should be able to handle multiple access tokens - That is, you will need to make a separate authorize request for each `audience` and use the resulting tokens against the relevant API.

---

<div class="post-metadata">

**Author:** ![mhautala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mhautala/32/33331_2.png) [@mhautala](https://community.auth0.com/u/mhautala)\
**Post date:** [January 27, 2023, 6:49am UTC](https://community.auth0.com/t/how-to-handle-multiple-apis-and-access-tokens-with-express-openid-connect/99025/4 "2023-01-27T06:49:21Z")

</div>

Yes I think it is pretty clear how to handle this in a generic way - if you would not use any SDK and build it your self with a few redirects, callbacks and a post request.

But a lot of our clients are using the express-openid-connect library and looks like it doesn’t at least document any way to handle two separate audiences. I guess the way to do it would to create two instances of the middleware and mount those in separate paths.

There is a comment implying something like this in this issue:

> <https://github.com/auth0/express-openid-connect/issues/263#issuecomment-884026700>
>
> \### Describe the problem you'd like to have solved
> 
> Need away to have multiple… instances of the middleware.
> I need to have 2 separate "logins" for admins and regulate users that both use the same SSO server, but have different client id's/secrets. Currently, the middleware extends \`req\` and \`res\` with \`req.oidc, ..\` that would be overridden if used side-by-side.
> 
> 
> \### Describe the ideal solution
> 
> Have multiple auth instances by having the ability to "customize" the names of the properties it extends e.g. have \`req.oidcAdmin\` or \`req\[Symbol.for('oidcAdmin')\]\`. 
> 
> Would imagine it would add a lot of complexity to code as they currently just check for the existence of \`req.oidc\`, having it configurable would require it to have the custom keys passed deeply into as config and have middlewares created from a factory function. And to avoid breaking changes the exported middlewares can be generated by the same factory function just passing it \`oidc\`.

---

<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:** [January 27, 2023, 11:44pm UTC](https://community.auth0.com/t/how-to-handle-multiple-apis-and-access-tokens-with-express-openid-connect/99025/5 "2023-01-27T23:44:12Z")

</div>

Hey @mhautala thanks for getting back to me - Thanks for sharing a link.

In that case, I recommend opening up an issue against the SDK itself to verify how or if you can handle multiple audiences/tokens. If you do please link that here if possible, thank you!

---

<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:** [February 11, 2023, 11:44pm UTC](https://community.auth0.com/t/how-to-handle-multiple-apis-and-access-tokens-with-express-openid-connect/99025/6 "2023-02-11T23:44:28Z")

</div>

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