Multiple baseUrls for the same express-openid-connect application

Problem statement

Is it possible to dynamically set baseURL on a per-request basis instead of defined statically one time?

The environments where we deploy our applications can be accessed from multiple clients. However, it looks like this library can’t handle this setup because we have to pass baseUrl to the middleware. But technically, the app instance has more than one baseUrl.

Troubleshooting

The same question was raised here:

Solution

You can set this up when the app mounts, e.g. if you have 2 base URLs for the internet and intranet:

import { auth } from 'express-openid-connect';

const configParams = {
 // ...my config
};

const internalBaseUrl = 'https://mycompany.local';;
const externalBaseUrl = 'https://mycompany.com';;

const internalAuth = auth({ ...configParams, baseURL: internalBaseUrl });
const externalAuth = auth({ ...configParams, baseURL: externalBaseUrl });

const auth0Mw = (req, res, next) => {
  const baseURL = `${req.protocol}://${req.header('x-forwarded-host') || req.get('host')}`;
  return (baseURL ===  internalBaseUrl ? internalAuth : externalAuth)(req, res, next);
};