nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.88k stars 3.51k forks source link

Add official Express.js support to auth/core #8257

Closed hillac closed 10 months ago

hillac commented 1 year ago

Description πŸ““

I've seen several issues / posts related to support for express.js and want to use it myself. I would like to propose adding express as an officially supported framework. Given the intention to support more frameworks, I think it would be worthwhile bringing direct support for Auth.js to the most popular back end javascript framework.

Here is an example implementation: rexfordessilfie/authjs-express-demo, which implements a translation layer for the web standard api request/response to express requests/responses, and express middleware for JWT authentication. So support seems quite straightforward.

I'd be willing help with this / add documentation for using express. If this feature is not accepted, I'm also happy to just write docs for using it like in authjs-express-demo if anyone else considers that worthwhile.

Some links to related issues:

For older versions:

On reddit:

How to reproduce β˜•οΈ

https://github.com/rexfordessilfie/authjs-express-demo

Contributing πŸ™ŒπŸ½

Yes, I am willing to help implement this feature in a PR

rexfordessilfie commented 1 year ago

Thanks for putting this issue together @hillac! As the creator of the demo repository I would also love to see official support and community efforts on the logic required to translate between Web Request/Response APIs and Express' APIs. I have brought it to a working state in my repository, but more hands and eyes on it would improve its quality and make it more useful to the community.

I think being added as a frameworks-express (e.g exposing a router that could be plugged into Express) could be the best path towards this, but also docs/a guide, or even a stand-alone library could also achieve similar! I would appreciate thoughts from maintainers and the community on this!

Thanks!

balazsorban44 commented 1 year ago

As far as I can tell, all we need is a wrapper around @auth/core that transforms Request -> Response to Node.js' req and res. The linked demo has that already, nice work!

If someone opens a PR, we could discuss further there.

As for documentation, we prefer JSDoc, that we can then use to auto-generate our docs site.

See https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/index.ts for an example. Rendered here: https://authjs.dev/reference/core

Looking forward to @auth/express

rexfordessilfie commented 1 year ago

Thank you for the comments and guidance @balazsorban44 πŸ™Œ! You’re right, the translation wrapper should be all that is needed, and is taken care of in the demo!

@hillac, what do you say to pairing together to put together a PR for review and discussion?

We can follow closely, the standard setup in frameworks-x directories, and follow the approach from the demo.

Also @balazsorban44, there were some more specific pieces such as having a body parser middleware in the app to make the translation fully possible. I will call these out in the JSDoc and PR to make sure we’d be taking the right approach there.

Thanks to you both again πŸ‘!

hillac commented 1 year ago

Yep sounds good.

binaryartifex commented 1 year ago

cant' tell you how much im looking forward to drop kicking passportjs in my nestjs application in favor of this....

matmar-91 commented 10 months ago

Thank you for working on this! ❀️ Any progress so far?🀞

hillac commented 10 months ago

Thank you for working on this! ❀️ Any progress so far?🀞

It's pretty much done https://github.com/nextauthjs/next-auth/pull/8354. But no word from the maintainers on it since August so I'm not sure when it will happen. You can just use the index and http-api-adapters files directly in your project for now, it's only a bit over 100 lines, so you can easily audit it.