Skip to content
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

Closed
lukevers opened this issue May 11, 2023 · 10 comments
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@lukevers
Copy link

lukevers commented May 11, 2023

Environment

  System:
    OS: macOS 13.2.1
    CPU: (8) arm64 Apple M1
    Memory: 798.67 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    Yarn: 3.2.4 - ~/.nvm/versions/node/v18.15.0/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
  Browsers:
    Chrome: 113.0.5672.92
    Firefox Developer Edition: 114.0
    Safari: 16.3

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:

OK so I initially had cookies().get('anything'); which did cause issues, but not on 13.4.2-canary.5. I do still get these errors, but not with cookies directly, I get them when I'm using getServerSession from import { getServerSession } from 'next-auth/next'; -- I'm not sure if this is a nextjs issue or a next-auth issue since cookies directly seem to work OK.

I have two pages that define two situations:

  • /issue1 - "individual server components" - The main issue here is that when we access cookies through a server component imported from a client component, it does not work. It works if we import it in a server component (page layout here) and pass it to the client component as a prop.
  • /issue2 - "shared server component" - The issue here is that when we access cookies through a server component imported from a client component, it does not work. It also does not work even if it was passed through to the client component if the server component is the same component

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)

@lukevers lukevers added the triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. label May 11, 2023
@balazsorban44
Copy link
Member

Duplicate of #6989.

It might be because of the runtime conditional require of next/headers here:

if (isRSC) {
options = Object.assign({}, args[0], { providers: [] })
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { headers, cookies } = require("next/headers")

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.

@lukevers
Copy link
Author

@balazsorban44 Is there a timeline for v5? In the meantime do you have a workaround example?

@balazsorban44
Copy link
Member

NextAuth.js v5 is planned for this or the coming week. Maybe getToken could be used if you do not use a database session.

@lukevers
Copy link
Author

@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...

@lukevers
Copy link
Author

@balazsorban44 OK, yeah, this feels odd. lukevers/next-644@ca99b9e

On there if you navigate to /local both of buttons will work, but the /issue1 and /issue2 routes that still pull from the library are not working.

@mwawrusch
Copy link
Contributor

So ran into this as well. Is there a pre release of NextAuth.js v5 - this is blocking us right now.

@subvertallchris
Copy link

subvertallchris commented Jun 11, 2023

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?

@lsagetlethias
Copy link

lsagetlethias commented Jun 16, 2023

In a server action, you can trick getServerSession to not detect RSC by building yourself the request and the response object, (reproducing internal next-auth code btw):

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.

@YashTotale
Copy link

@lsagetlethias This was the only solution that worked for me!

@rodolfoag
Copy link

@lsagetlethias this solution worked for me to. Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

7 participants