Support Next.js 12 Middleware
See original GitHub issueDescription 📓
After watching the recent Nextjs conf, I’m wondering if there’s a way to move authentication api (here it’s the /session
endpoint) to run on the edge instead that on server. Looking at this very basic example
https://youtu.be/PWi-49ODKAs?t=3203
maybe there’s a way to take advantage of this in NextAuth too, even if in an opt-in way.
How to reproduce ☕️
Nothing to reproduce
Contributing 🙌🏽
Yes, I am willing to help implement this feature in a PR
Issue Analytics
- State:
- Created 2 years ago
- Reactions:56
- Comments:27 (18 by maintainers)
Top Results From Across the Web
Advanced Features: Middleware
Middleware allows you to run code before a request is completed, then based on the incoming request, you can modify the response by...
Read more >Next.js 12.2 Overview: Middleware, Layouts, and more
Next. js 12.2 introduces stable Middleware and On-Demand ISR, experimental Edge SSR and API Routes, and more.0:00 Intro1:50 Agenda2:30 ...
Read more >How to Add New Middleware Feature with Next.js ... - YouTube
How to Add New Middleware Feature with Next. js 12.2 In Application - For BeginnersHey everyone welcome back to daily tuition.
Read more >AWS Amplify Hosting announces support for Next.js 12 and ...
js 12 and 13 support, including middleware, incremental static regeneration (ISR), and image optimization. With this release, AWS Amplify ...
Read more >The What, When, Why And How Of Next.js' New ...
Next.js' middleware allows you to create functions that execute after a user's request is made and before the request is completed — in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am proposing a
withAuth
method, that could add authentication for the whole site (or any subdirectory) with a single line! Please check out #3657 and tell me what you think!pages/_middleware.js:
I’ve gotten multiple related questions in the last 24 hours, so I will look into this more.
We are using
jose
under the hood which relies on the Web Cryptography API and Cloudflare Workers (the stuff behind Edge Functions) doesn’t support all of it (yet?) See: https://github.com/panva/jose/issues/265#2857 is definitely the first step towards support here, but I’ll have to check if it is important for us that those unimplemented things work in CF Workers or not.
If not, then exposing a new method for Middlewares would be very easy.
It can be much trickier otherwise because I really like
jose
. It is a high-quality library with stellar maintenance.Most of our users who are asking about this are probably interested in a way that would make it possible to check the user session, and if it is valid, proceed to the page, otherwise redirect to the login page.
We have to consider two scenarios: