I am trying to use this library (express-openid-connect) to provide authentication backend with "simple setup" but I am getting a simple error when calling the oidc object from express.Request:
app.get("/", (req: express.Request, res: express.Response) => {
res.send(req.oidc.isAuthenticated() ? "Hi user" : "Hi guest");
});
Of course, I am getting this error:
Property 'oidc' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.ts(2339)
I have tried so far implementing an interface to replace express.Request but I get more errors:
interface OIDRequest extends express.Request {
oidc: OpenidRequest
}
app.get("/", (req: OIDRequest, res: express.Response) => {
res.send(req.oidc.isAuthenticated() ? "Hi user" : "Hi guest");
});
And now:
No overload matches this call.
Overload 1 of 4, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Express', gave the following error.
Argument of type '(req: OIDRequest, res: express.Response) => void' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
Types of parameters 'req' and 'req' are incompatible.
Property 'oidc' is missing in type 'Request<ParamsDictionary, any, any, ParsedQs>' but required in type 'OIDRequest'.
Overload 2 of 4, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>[]): Express', gave the following error.
Argument of type '(req: OIDRequest, res: express.Response) => void' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>'.
Type '(req: OIDRequest, res: express.Response) => void' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
Overload 3 of 4, '(path: PathParams, subApplication: Application): Express', gave the following error.
Argument of type '(req: OIDRequest, res: express.Response) => void' is not assignable to parameter of type 'Application'.
Type '(req: OIDRequest, res: Response) => void' is missing the following properties from type 'Application': init, defaultConfiguration, engine, set, and 61 more.ts(2769)
Just in case you ask my app was configured in this way:
import express from 'express';
import { auth, ConfigParams, OpenidRequest } from 'express-openid-connect';
require('dotenv').config();
const app = express();
const config: ConfigParams = {
authRequired: false,
auth0Logout: true,
issuerBaseURL: process.env.ISSUER_BASE_URL,
baseURL: process.env.BASE_URL,
clientID: process.env.CLIENT_ID,
secret: process.env.SECRET,
};
app.use(auth(config));
interface OIDRequest extends express.Request {
oidc: OpenidRequest
}
app.get("/", (req: OIDRequest, res: express.Response) => {
res.send(req.oidc.isAuthenticated() ? "Hi user" : "Hi guest");
});
app.get("/profile", (req: express.Request, res: express.Response) => {
res.json(req.oidc.user);
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Listening to port: ${port}`);
});
I found some answers before I asked here but those didn't work for this scenario. How should I call oidc from express.Request object?