`getSession` fails during build when used in middleware
See original GitHub issueEnvironment
react 17.0.2 next-auth 4.3.3 next 12.1.5
Reproduction URL
N/A
Describe the issue
getSession
does not work in nextjs middlewares. Or rather, it works during development but fails when trying to build the project, with the following trace:
./node_modules/@babel/runtime/helpers/isNativeFunction.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware pages/planning/_middleware
Import trace for requested module:
./node_modules/@babel/runtime/helpers/wrapNativeSuper.js
./node_modules/next-auth/core/errors.js
./node_modules/next-auth/lib/logger.js
./node_modules/next-auth/react/index.js
./src/server/auth_middleware.ts
./src/pages/overview/_middleware.ts
This seems to be a “known” issue, #4302, but that issue was just closed out of hand, even though it was “supposed to work”.
According to the documentation, we are supposed to use withAuth
but this requires the JWT session strategy, which I’m not using.
What is the recommended approach here?
How to reproduce
- Create new next project with next-auth and session auth and any adapter
- Try to use
getSession
in middlewares - Try to build the project
Expected behavior
It doesn’t fail to build.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to get session in Next.js middleware? (error in deploy)
I want unauthorized people redirected to '/enter' page by using next-auth session. So I used getSession. Is it wrong way to get session...
Read more >How would I use getsession in a middleware file : r/nextjs
I have made a folder with the routes I want to page protect, I am using next-auth and getSession returns null. How wouls...
Read more >Using the getSession function | SuperTokens Docs
However, verifySession is a middleware that returns a reply directly to the frontend if the input access token is invalid or expired. On...
Read more >A Substitution API Reference
A.2.13 getDataType() Method. Use to return the syntax creating a attribute of a given datatype. Usage public java.lang.String getDataType ...
Read more >Express cookie-session middleware
The following points can help you choose which to use: cookie-session does not require any database / resources on the server side, though...
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
Sorry, the reason is that getSession is not compatible with the Edge Runtime, but we are working on it.
The reason is that some of the core relies in Node.js, plus there isn’t many Edge compatible database solutions yet, as far as I know.
This is briefly mentioned under caveats of the Middleware documentation.
https://next-auth.js.org/configuration/nextjs#caveats
So, for who is using a non-JWT strategy, we need just wait getSession get compatible with edge functions? Or have another way?