-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling getServerSession
in Server Action triggers error "Invariant: Method expects to have requestAsyncStorage, none available"
#7523
Comments
Duplicate of #6989. It might be because of the runtime conditional require of next-auth/packages/next-auth/src/next/index.ts Lines 188 to 192 in cca94bf
Unfortunately, we cannot get rid of this as of now, since it would be a breaking change for Next.js 12 users. NextAuth.js v5 will bump the required Next.js version and this problem should be solved then. |
@balazsorban44 Is there a timeline for v5? In the meantime do you have a workaround example? |
NextAuth.js v5 is planned for this or the coming week. Maybe |
@balazsorban44 So I just copied all of that code into a file locally and wanted to debug it some more. I am wondering if it has anything to do with the next bundler because this works exactly as is with no modifications. Let me see if I can pull this into my example repository... |
@balazsorban44 OK, yeah, this feels odd. lukevers/next-644@ca99b9e On there if you navigate to |
So ran into this as well. Is there a pre release of NextAuth.js v5 - this is blocking us right now. |
You can work around this by passing the server action as a prop from a server component into a client component. This isn't always practical. Any thoughts on when v5 will be released? |
In a server action, you can trick import { cookies, headers } from "next/headers";
import { getServerSession } from "next-auth";
export const getServerActionSession = () => {
const req = {
headers: Object.fromEntries(headers()),
cookies: Object.fromEntries(
cookies()
.getAll()
.map(c => [c.name, c.value]),
),
} as any;
const res = {
getHeader() {
/* empty */
},
setCookie() {
/* empty */
},
setHeader() {
/* empty */
},
} as any;
return getServerSession(req, res, authConfig); // authConfig is your [...nextAuth] route config
} Maybe in the future, Next will give access somehow to server action's request and response, but in the meanwhile, that's what worked for me. |
@lsagetlethias This was the only solution that worked for me! |
@lsagetlethias this solution worked for me to. Thx! |
Environment
Reproduction URL
https://github.com/lukevers/next-644
Describe the issue
I came across this issue vercel/next.js#46356 and thought the issue was nextjs with cookies, and confirmed that on the most recent release of next. I updated to
13.4.2-canary.5
and saw the issue subsided with cookies directly. I tested with next-auth as a replacement to see if that resolved the issues, and saw the same issue here while the next cookie example was no longer an issue.Bringing over my comment from here:
How to reproduce
Download my example repo, run it, and follow the instructions.
Expected behavior
Instead of causing an error, it should let me get access to the session (in the repo example it's null, but that's fine/expected in that example)
The text was updated successfully, but these errors were encountered: