Git Product home page Git Product logo

Comments (4)

nfadili avatar nfadili commented on June 22, 2024 2

Hi @nfadili

It is my understanding that the cors middleware doesn't affect this. Whether it comes before or after the auth middleware, it is only ever attaching CORS related headers to responses, nothing more. Please let me know if I am incorrect there!

The default value for preflightContinue is false (see https://expressjs.com/en/resources/middleware/cors.html#configuration-options) which means that preflight requests will terminate at the cors middleware by default.

Also, the cors middleware docs recommend you put the cors middleware before other routes (see https://expressjs.com/en/resources/middleware/cors.html#enabling-cors-pre-flight). Which is what we do in the example app https://github.com/auth0/node-oauth2-jwt-bearer/blob/main/packages/examples/express-api.ts#L20

Ahhhh I see now. Thanks for the explanation and links, the current behavior makes total sense 👍 please feel free to close this out! 👍

from node-oauth2-jwt-bearer.

adamjmcgrath avatar adamjmcgrath commented on June 22, 2024

Hi @nfadili - thanks for raising this

I see what you mean and I know we put some logic in express-jwt to handle this, but I'd rather just expect the user to put their cors handling middleware before their auth handling, this seems more logical to me than making it possible to put them out of order.

app.use(cors());
app.use(auth());

I also feel a little uncomfortable about letting the request decide if it should bypass authentication checks (although this is probably overkill)

from node-oauth2-jwt-bearer.

nfadili avatar nfadili commented on June 22, 2024

Hey @adamjmcgrath thanks for the quick reply!

It is my understanding that the cors middleware doesn't affect this. Whether it comes before or after the auth middleware, it is only ever attaching CORS related headers to responses, nothing more. Please let me know if I am incorrect there! In my mind, the root of the issue is that the auth middleware checks for credentials (in headers, body, and query) for every request it is ran for. Requests that are CORS-preflight OPTIONS requests will not contain credentials because the CORS spec advises against it, so the example usages that show app.use(auth({...}) will not work with cross-origin requests.

I do agree that it makes sense for this lib to not be opinionated on what requests to bypass auth on 😄 so I think the existing behavior makes sense! It is an important behavioral difference from the last lib Auth0 maintained (express-jwt) so hopefully this will help someone out if they are going through the migration!

from node-oauth2-jwt-bearer.

adamjmcgrath avatar adamjmcgrath commented on June 22, 2024

Hi @nfadili

It is my understanding that the cors middleware doesn't affect this. Whether it comes before or after the auth middleware, it is only ever attaching CORS related headers to responses, nothing more. Please let me know if I am incorrect there!

The default value for preflightContinue is false (see https://expressjs.com/en/resources/middleware/cors.html#configuration-options) which means that preflight requests will terminate at the cors middleware by default.

Also, the cors middleware docs recommend you put the cors middleware before other routes (see https://expressjs.com/en/resources/middleware/cors.html#enabling-cors-pre-flight). Which is what we do in the example app https://github.com/auth0/node-oauth2-jwt-bearer/blob/main/packages/examples/express-api.ts#L20

from node-oauth2-jwt-bearer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.