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

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.

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 :frowning:

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.

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?

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. 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.

1 Like

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

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