question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`getSession` fails during build when used in middleware

See original GitHub issue

Environment

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

  1. Create new next project with next-auth and session auth and any adapter
  2. Try to use getSession in middlewares
  3. Try to build the project

Expected behavior

It doesn’t fail to build.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
balazsorban44commented, Apr 26, 2022

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

0reactions
HenriqueRamos13commented, Nov 27, 2022

So, for who is using a non-JWT strategy, we need just wait getSession get compatible with edge functions? Or have another way?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found