TypeScript + Express middleware types

  • Which SDK this is regarding: express-openid-connect
  • SDK Version: 2.2.1
  • Platform Version: Node 14.15.5

Context:
I am building an express app, I plan to separate it conceptually into two components

  1. Landing page
  2. Admin

Admin requires auth, while the landing page doesn’t. I am using the express Router to configure my routes. So I construct two new routers which can accept middleware just as the express app itself can. For the admin section I am able to gate it’s routes using the requiresAuth middleware exposed by you fine folk!!

Problem
When I try and access req.ocid.user TypeScript doesn’t recognize the field oidc

My express app is setup as follows (cut down to only include relevant code):

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

import { funnelRouter, adminRouter } from './routes';

const app = express();

app.use(auth(AUTH_CONFIG));
app.use('/', funnelRouter);
app.use('/admin', adminRouter);

export const start = () => {
  app.listen(PORT, () => {
    console.log(`Listening on localhost:${PORT}`);
  });
};

AUTH_CONFIG

export const AUTH_CONFIG = {
  authRequired: false,
  auth0Logout: true,
  secret: process.env.AUTH_0_SECRET,
  baseURL: 'http://localhost:3000',
  clientID: my-id,
  issuerBaseURL: 'my-url',
};

Admin router

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

const router = express.Router();

router.use(requiresAuth());

router.get('/', (req, res) => {
  res.send(JSON.stringify(req.oidc.user));
});

export default router;

Hi @benjamnkovacs ,

Just want to make sure this isn’t a mistype, as you have ocid and it should be oidc

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.