auth0 / express-openid-connect

An Express.js middleware to protect OpenID Connect web applications.
MIT License
460 stars 139 forks source link

use `express()` instead of `express.Router()` #604

Open phonzammi opened 5 months ago

phonzammi commented 5 months ago

By submitting a PR to this repository, you agree to the terms within the Auth0 Code of Conduct. Please see the contributing guidelines for how to create and submit a high-quality PR for this repo.

Description

When we want to integrate/use this library with other frameworks (e.g. unjs/h3), we need to create an express app first than we can use this library. In this case we have to install express package too. For example when using with unjs/h3

import {
  createApp,
  fromNodeMiddleware,
} from "h3";
import express from "express";

const app = createApp();
const expressApp = express();

app.use(fromNodeMiddleware(expressApp.use(auth(config))));

If we use it like this (without an express app) :

app.use(fromNodeMiddleware((auth(config)))

Than it will throws this errors

/home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/layer.js:95
    fn(req, res, next);
    ^
TypeError: req.get is not a function
    at /home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express-openid-connect@2.17.1_express@4.18.3/node_modules/express-openid-connect/lib/appSession.js:289:37
    at Layer.handle [as handle_request] (/home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/index.js:328:13)
    at /home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/index.js:346:12)
    at next (/home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/index.js:280:10)
    at Function.handle (/home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/index.js:175:3)
    at router (/home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/express@4.18.3/node_modules/express/lib/router/index.js:47:12)
    at file:///home/phonzammi/Documents/vike-dev/practices/auth0/vike-h3-auth0/node_modules/.pnpm/h3@1.11.1/node_modules/h3/dist/index.mjs:2285:24
    at new Promise (<anonymous>)

With this changes, we can use this library with other frameworks without having to create an express app

Testing

I have successfully tested this changes with fastify and unjs/h3

Checklist

phonzammi commented 4 months ago

Hi @frederikprijck, I'm sorry to tag you.

Would you mind reviewing this PR ? I'd like to know what do you think of this. is this a "breaking changes"? Does Auth0 team has any plan to support more node js framework or more server environment?