I’m building a web app for multy users with one or more theia-backend behind reverse proxy.
The use case is that user open theia app from web app A, before thiea app loads in browser, I want to intercept the request and make some authentication.I searched the community as well as github, find that BackendApplicationContribution provide hook to acheive this.
When I add my own middleware, the browser apps goes to 404.
Below is my code
import {BackendApplicationContribution} from "@theia/core/lib/node";
import {MaybePromise} from "@theia/core";
import express = require('express');
import {injectable} from "inversify";
@injectable()
export class ThiauuobaAuthContribution implements BackendApplicationContribution {
configure(app: express.Application): MaybePromise<void> {
console.log("ThiauuobaAuthContribution.configure");
const middleware = this.expressMiddleware.bind(this);
app.use(middleware);
console.log("after app.use")
const routerStack = app._router.stack as any[];
const layer = routerStack.splice(routerStack.findIndex(r => r.handler === middleware), 1);
routerStack.splice(routerStack.findIndex(r => r.name === "expressInit") + 1, 0, ...layer);
}
protected expressMiddleware(req: express.Request, res: express.Response, next: express.NextFunction): void {
console.log("ThiauuobaAuthContribution.expressMiddleware");
if (this.validateToken()) {
next()
} else {
console.log("ThiauuobaAuthContribution.expressMiddleware: invalid token");
res.sendStatus(403);
}
}
validateToken(): boolean {
return true;
}
}
I see https://github.com/eclipse-theia/theia/blob/master/packages/core/src/electron-node/token/electron-token-backend-contribution.ts use this hook, but my apps failed on this.
I also read this and this and this, none of them work.
Anyone helps? Thanks.