# Express-openid-connect two different “instances” based on the a condition in the request

**URL:** <https://community.auth0.com/t/express-openid-connect-two-different-instances-based-on-the-a-condition-in-the-request/122161>\
**Category:** Get Help\
**Tags:** express-openid-connect, sdks-quickstarts\
**Created:** [December 1, 2023, 4:16pm UTC](https://community.auth0.com/t/express-openid-connect-two-different-instances-based-on-the-a-condition-in-the-request/122161 "2023-12-01T16:16:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user-bks](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/user-bks/32/50274_2.png) [@user-bks](https://community.auth0.com/u/user-bks)\
**Post date:** [December 1, 2023, 4:16pm UTC](https://community.auth0.com/t/express-openid-connect-two-different-instances-based-on-the-a-condition-in-the-request/122161/1 "2023-12-01T16:16:18Z")

</div>

Hello,  
I have a problem, I don’t know if this is the right place or format to ask but it would be very greatfull if you could help me.

I need two different “instances” based on the a condition in the request.

Since oidc is stateless as far as I know I was hopful to get this working.

So I was trying to do something like this.

```auto
const app = express();

app.use((req, res, next)=>{

if(condition(req)){

return auth(config1)(req, res, next);

} else {

return auth(config2)(req, res, next);

}

});

```

but it doesn’t work ☹

I narrowed down my problem and now have the info that

`app.use(auth(config1));`

works but

`app.use((req, res, next)=>{return auth(config1)(req, res, next);});`

doesn’t. I get error “

> BadRequestError: checks.state argument is missing at responseContext.callback (context.js:366:15)

”

Does someone know the cause of this problem. I am kinda stuck.

Also could this work or is this doomed from the start because of some detail in the oidc protocol or the implementation that I am overlooking.

---

<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:** [December 6, 2023, 7:29pm UTC](https://community.auth0.com/t/express-openid-connect-two-different-instances-based-on-the-a-condition-in-the-request/122161/3 "2023-12-06T19:29:33Z")

</div>

Hi @user-bks,

Welcome to the Auth0 Community!

This could cause some unforeseen issues. There may be a better way here…can you give us some more information about why you’re doing this?

---

<div class="post-metadata">

**Author:** ![user-bks](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/user-bks/32/50274_2.png) [@user-bks](https://community.auth0.com/u/user-bks)\
**Post date:** [December 9, 2023, 2:25pm UTC](https://community.auth0.com/t/express-openid-connect-two-different-instances-based-on-the-a-condition-in-the-request/122161/4 "2023-12-09T14:25:49Z")

</div>

Hello,  
I am not sure if the “fix” actually was the problem or if it was another side-effect that changed at the same time but I managed to make it work.

For me the key finding was [this comment in a GitHub issue](https://github.com/auth0/express-openid-connect/issues/200#issuecomment-793668876). After refactoring the middle-ware function out of my main file and creating the auth only two time not for every request (like the code in the comment dose) it magically worked and now I am just not touching it anymore.

---

<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:** [December 11, 2023, 4:06pm UTC](https://community.auth0.com/t/express-openid-connect-two-different-instances-based-on-the-a-condition-in-the-request/122161/5 "2023-12-11T16:06:27Z")

</div>

Okay, let us know if you run into anything else.

---

<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:** [December 25, 2023, 4:06pm UTC](https://community.auth0.com/t/express-openid-connect-two-different-instances-based-on-the-a-condition-in-the-request/122161/6 "2023-12-25T16:06:40Z")

</div>

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