'express-openid-connect with TypeScript

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?



Solution 1:[1]

We recently switched to express-openid-connect and have had to use this method to interact with OIDC.

    app.get('/api/me', async (req, res) => {
        const userInfo = await req['oidc']?.fetchUserInfo();
        res.json({
            info: req['oidc']?.user,
            userInfo
        });
    });

Solution 2:[2]

I have recently used express-openid-connect in my project and, this is how I did it. Basically, I have created a namespace.d.ts file in my project root directory with the below code.

import { Request } from 'express';
import { OpenidRequest } from 'express-openid-connect';
declare module 'express' {
    interface Request {
        openId?: OpenidRequest
    }
}

then use it like this:

app.get("/profile", (req: Request, res: Response) => {
   const user = req.openId?.oidc.user
    res.status(200).json({user});
});

Solution 3:[3]

As of Early 2022 when you try to import import { OpenidRequest } from 'express-openid-connect'; you get a warning that this is deprecated.

@deprecated use the native the Request interface of express instead; it has been extended > and now includes a built in oidc param.

So you should just be able to do this :

app.use(auth());

app.get('/profile', (req: Express.Request, res: Express.Response) => {
  const user = req.oidc.user;
  ...
})

Solution 4:[4]

Is there any more documentation about this? I cant find a way to make it work either...

Property 'oidc' does not exist on type 'Request'.ts(2339)

I've noticed the deprecated warning but it does not help a lot... I'm using Nuxt2 with Express.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AltiView
Solution 2 sajan
Solution 3 Kyle Bennett
Solution 4