next-auth: Auth v5 - Prisma Adapter with Google Provider on Runtime Edge - [TypeError]: immutable
Adapter type
Environment
System: OS: macOS 14.1.1 CPU: (8) x64 Intel® Core™ i5-8259U CPU @ 2.30GHz Memory: 44.29 MB / 8.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 20.10.0 - /usr/local/bin/node npm: 10.2.4 - /usr/local/bin/npm Browsers: Chrome: 119.0.6045.199 Safari: 17.1 npmPackages: @auth/prisma-adapter: ^1.0.8 => 1.0.8 next: ^14.0.3 => 14.0.3 next-auth: ^5.0.0-beta.4 => 5.0.0-beta.4 react: ^18 => 18.2.0
Reproduction URL
https://github.com/riccardolinares/next-auth-v5
Describe the issue
I am currently working on a project that utilizes the Prisma adapter in conjunction with Prisma Accelerate. However, I am encountering a persistent and somewhat random error related to the Prisma Adapter, then a Session error and at the end I get an Error [TypeError]: immutable when I try to login again using the google provider.
I am not sure if it’s related mainly to the adapter itself or to the Google Provider. It is driving me crazy!
The issue mainly arises when a user is logged in for an unspecified duration. Unfortunately, the error’s sporadic nature makes it difficult to reproduce reliably, complicating the troubleshooting process.
Key points to note:
- Runtime Environment: The project is built on Next.js version 14, specifically using the Runtime Edge configuration.
- Error Description: The error associated with the Prisma Adapter surfaces randomly during a user’s session. Once it appears, it persists and affects the user experience.
- Temporary Resolution: The only way I have found to temporarily resolve the issue is by clearing the application’s cache and deleting the .next folder. Then after a few minutes I am able to login again… However, this is not a viable long-term solution.
Attempts to Resolve:
I have tried to monitor the application to pinpoint the cause but due to the randomness of the error, this approach has not yielded any significant insights. I have also checked for any potential updates or patches for Prisma and Next.js that might address this issue but have not found any relevant updates.
Additional Information:
There are no clear patterns or specific user actions that seem to trigger this error. The application logs do not provide detailed information that could help in isolating the problem. I am looking for any guidance or suggestions on how to approach this issue, especially from others who might have encountered similar problems with Prisma Adapter and Prisma Accelerate on Next.js Runtime Edge.
Here is the log of the error:
[auth][error] AdapterError: Read more at https://errors.authjs.dev#adaptererror
[auth][cause]: PrismaClientUnknownRequestError:
Invalid `prisma.session.findUnique()` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(PostgresError { code: "26000", message: "prepared statement \"s489\" does not exist", severity: "ERROR", detail: None, column: None, hint: None }), transient: false })
at Ei.handleRequestError (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:84:7381)
at Ei.handleAndLogRequestError (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:84:6595)
at Ei.request (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:84:6293)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async u (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:89:10383)
at async getSessionAndUser (webpack-internal:///(rsc)/./node_modules/@auth/prisma-adapter/index.js:250:36)
at async acc.<computed> (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/init.js:169:24)
at async Module.session (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/actions/session.js:84:30)
at async AuthInternal (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/index.js:50:24)
at async Auth (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/index.js:123:29)
at async ChatPage (webpack-internal:///(rsc)/./src/app/(private)/chat/[id]/page.tsx:43:21)
[auth][details]: {}
[auth][error] SessionTokenError: Read more at https://errors.authjs.dev#sessiontokenerror
[auth][cause]: PrismaClientUnknownRequestError:
Invalid `prisma.session.findUnique()` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(PostgresError { code: "26000", message: "prepared statement \"s489\" does not exist", severity: "ERROR", detail: None, column: None, hint: None }), transient: false })
at Ei.handleRequestError (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:84:7381)
at Ei.handleAndLogRequestError (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:84:6595)
at Ei.request (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:84:6293)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async u (/Users/ac/RiccardoProjects/secondsoul/node_modules/@prisma/client/runtime/edge.js:89:10383)
at async getSessionAndUser (webpack-internal:///(rsc)/./node_modules/@auth/prisma-adapter/index.js:250:36)
at async acc.<computed> (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/init.js:169:24)
at async Module.session (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/actions/session.js:84:30)
at async AuthInternal (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/index.js:50:24)
at async Auth (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/index.js:123:29)
at async ChatPage (webpack-internal:///(rsc)/./src/app/(private)/chat/[id]/page.tsx:43:21)
[auth][details]: {}
…After multiple errors like the previous one, if I try to login again, then I get:
error { error: 'invalid_grant', error_description: 'Bad Request' }
[auth][error] CallbackRouteError: Read more at https://errors.authjs.dev#callbackrouteerror
[auth][cause]: Error: TODO: Handle OIDC response body error
at handleOAuth (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/actions/callback/oauth/callback.js:80:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Module.callback (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/actions/callback/index.js:33:41)
at async AuthInternal (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/lib/index.js:42:24)
at async Auth (webpack-internal:///(rsc)/./node_modules/next-auth/node_modules/@auth/core/index.js:123:29)
at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:215:37)
[auth][details]: {
"provider": "google"
}
⨯ Error [TypeError]: immutable
at _Headers.delete (file:///Users/ac/RiccardoProjects/secondsoul/node_modules/next/dist/compiled/edge-runtime/index.js:1:657096)
at runWithTaggedErrors (file:///Users/ac/RiccardoProjects/secondsoul/node_modules/next/dist/server/web/sandbox/sandbox.js:106:43)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async DevServer.runEdgeFunction (file:///Users/ac/RiccardoProjects/secondsoul/node_modules/next/dist/server/next-server.js:1169:24)
at async NextNodeServer.handleCatchallRenderRequest (file:///Users/ac/RiccardoProjects/secondsoul/node_modules/next/dist/server/next-server.js:242:37)
at async DevServer.handleRequestImpl (file:///Users/ac/RiccardoProjects/secondsoul/node_modules/next/dist/server/base-server.js:798:17)
Thank you for your attention to this matter. Any assistance or insights into this issue would be greatly appreciated.
How to reproduce
Unfortunately, the error’s sporadic nature makes it difficult to reproduce reliably, complicating the troubleshooting process.
Expected behavior
Keep the user logged in and don’t give the error “immutable”
About this issue
- Original URL
- State: open
- Created 7 months ago
- Reactions: 25
- Comments: 30 (5 by maintainers)
Commits related to this issue
- fix immutable ERROR refer to https://github.com/nextauthjs/next-auth/issues/9300#issuecomment-2026693670 — committed to redsubmarine/next-auth by yangoneseok 3 months ago
I’m facing the same error right now.
I had to comment out the second line for it to work for me (export const runtime = “edge”; // optional)
facing same issue with edge on
5.0.0-beta.4, on5.0.0-beta.3its working fine.[EDIT]: My solution also doesn’t work… I will still keep what I tried, however, my ultimate solution was to switch to Supabase Auth.
…
After spending hours, I found a solution that worked for me!!
First of all, let’s discuss what might cause the issue
Error [TypeError]: immutable. As my understanding, the reason is that while NextAuth v5 is fully Edge-compatible, some providers (e.g., Primsa) are not.Luckily, there is a workaround for that. I will not explain each step here since it is a relatively long process. However, you can easily follow the two guides I will provide.
Important points I encountered:
DATABASE_URLin your.envfile with that new connection string.import { PrismaClient } from "@prisma/client/edge";I hope this helps!
The problem is not related to Google Provider. I am trying with other provider and getting the same error. It seems to be related to the Edge Runtime
I am experiencing this also with 5.0.0-beta.3 as long as I am running on Vercel edge.
Are there any docs on which adapters are edge-compatible?
This error occurs because NextAuth is Edge compatible, but some of the database adapters, such as Prisma, are not. If any of your files are configured to have a runtime on Edge, you’ll likely encounter this error.
The solution is to locate the route.ts file in your app/api/[…nextauth] folder and set the runtime to nodejs or completely remove the runtime variable from your route.ts file.
Your app/api/auth/[…nextauth]/route.ts should resemble the following:
export { GET, POST } from “@/auth”; export const runtime = “nodejs”; // You can optionally remove this, but if it’s set to ‘edge’, there will be an error when using edge incompatible adapters.
Yeah so you also can’t be doing any database actions in the middleware which is happening due to the fact that yuo’ve got the database session strategy selected. If yuo change this to
"jwt"it should work with@prisma/client@5.9.1andnext-auth@5.0.0-beta.9. Also@auth/prisma-adapteris up to1.3.3now, I’d upgrade to that as well 👍I have this issue too
I fixed it: https://authjs.dev/reference/adapter/prisma#create-the-prisma-schema-from-scratch
Please check and verify that the Account model is exactly like that. The fields need to be snake_case while providerAccountId must be camelCase. They were all camelCase for me
Also why so many inconsistencies? All models use camelCase notation while only
Accountmodel uses snake_case notation, and not even fully but partially. This is so stupid. And it’s even more stupid that this HAS TO be exactly like that, no flexibility for me to modify those fields, not even change their casing conventionI’m getting the same error. Trying to login with my github and the error is
more logs
Using
5.0.0-beta.4, not usingDIRECT_URLWas getting this error before, but now I’m getting the following error:
TypeError: "response" must be an instance of ResponseUsing drizzle, node 18, and pnpm. Tried npm and node 16/20 as well
Note: I am using prisma accelerate as database url and I have also a database direct url in my env.