For a while now, I've been facing a block with no resolution in sight for this particular issue. Hopefully, someone out there can lend a hand.
Background
I have a TS express application running on Firebase functions. Additionally, I utilize a custom domain (hosted via Firebase Hosting) that directs to my function so I can access it through a custom domain api.myserver.com
. My application caters to multiple businesses, each having its dedicated URL like https://company.api.myserver.com
.
One of my clients requested support for SSO integration with Okta. Following Okta's tutorial/sample project at this link, it involves a simple jsExpress app using express-openid-connect
.
In my local environment, I simulate the business-specific URL by appending the company prefix to the URL provided by Firebase emulator, e.g.
http://company.api.localhost:5001/project-id/us-central1/function-name
Everything runs smoothly in localhost; I successfully connect to my Okta environment via SSO, receive the /callback
request post-login, and get redirected to the target URL as expected.
The Issue
Upon deploying to production and testing it on my actual domain (along with the company subdomain) https://company.api.myserver.com
, I encounter an unexpected behavior. While I can login with my credentials, the execution flow lands me in an infinite loop - redirecting back to the /login
endpoint within auth(config)
, rather than progressing further.
Following extensive debugging, here are a couple of key observations:
- Cookies are being received at my
/callback
endpoint but they do not persist upon reaching the target URL, resulting in emptyreq.cookies
. - To investigate a potential URL mismatch due to Firebase provisioning, I added logging to print the URL received using
, which surprisingly reflects the URL issued by Firebase instead of my custom domain! For instance,req.protocol + "://" + req.get("host") + req.originalUrl
is seen instead ofhttps://us-central1-project-id.cloudfunctions.net/login
company.api.myserver.com
(uncertainty prevails whether this triggers cookie persistence issues).
Desperate after exhausting forums, debugging sessions, and consultations even to chatGPT, I turn to this platform for assistance. Any insights?
Below are snippets of relevant code segments along with explanatory comments:
server.ts
export const app = express();
app.set("trust proxy", true);
// various middleware configurations
app.use(function (req, res, next) {
// user authentication setup
});
app.use(genAuthenticateUser);
app.use(genValidateEnterprise);
authConfig()
async function authConfig(
req: express.Request,
res: express.Response,
next: express.NextFunction
): Promise<void> {
// cookies verification
// authentication handling logic
// initial setup for authentication middleware
const authMiddleware = auth(config);
authMiddleware(req, res, next); // redirection point
}