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

[NEXT-1290] getServerSession in ServerAction #50658

Closed
1 task done
bfourgeaud opened this issue Jun 1, 2023 · 7 comments · Fixed by #51367
Closed
1 task done

[NEXT-1290] getServerSession in ServerAction #50658

bfourgeaud opened this issue Jun 1, 2023 · 7 comments · Fixed by #51367
Assignees
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@bfourgeaud
Copy link

bfourgeaud commented Jun 1, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 18.12.1
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.5-canary.3
      eslint-config-next: 13.4.4
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

https://github.com/bfourgeaud/server-actions-session-repro

To Reproduce

1 - Login (with any credentials)
2 - Click "Server Action" button
3 - See error on server side (terminal)

Describe the Bug

Whenever I try to use getServerSession from next-auth in a server action, the server throws an error :
Error: Invariant: Method expects to have requestAsyncStorage, none available

On the other hand, on any other server component (home page of reproduction for example), there is no problem retrieving the current session.

Expected Behavior

It should be possible to retrieve session information in server-actions the same way we retrieve them in any server-component.

Which browser are you using? (if relevant)

chrome

How are you deploying your application? (if relevant)

next dev

NEXT-1290

@bfourgeaud bfourgeaud added the bug Issue was opened via the bug report template. label Jun 1, 2023
@andrewmcoupe
Copy link

@bfourgeaud Have you tried passing the action as a prop to the client component?

https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#props

@chungweileong94
Copy link
Contributor

@bfourgeaud Have you tried passing the action as a prop to the client component?

https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#props

Yes, this will fix the issue. I'm not sure if this is a bug or by design.

@bfourgeaud
Copy link
Author

✔️ Ok it works when passing down props instead of making the call inside the server action.

But it would simplify props handling if it were possible. For now I have to pass down session 3 levels down (page -> button -> serverAction), when I simply could make a call inside server action.

Before closing, maybe wait if that is intended behaviour or bug ?

@shuding shuding added the linear: next Confirmed issue that is tracked by the Next.js team. label Jun 15, 2023
@shuding shuding self-assigned this Jun 15, 2023
@shuding shuding changed the title getServerSession in ServerAction [NEXT-1290] getServerSession in ServerAction Jun 15, 2023
@mirshko
Copy link

mirshko commented Jun 15, 2023

Im having the same issue, but it seems to be from the next/headers cookies package, not next-auth, or in my case auth-helpers from Supabase

@DenisBessa
Copy link

For me, this problem only happens with a "use server" on the top of the file. If the function is declared within a server component and is marked with "use server", the problem is gone.

@kodiakhq kodiakhq bot closed this as completed in #51367 Jun 16, 2023
kodiakhq bot pushed a commit that referenced this issue Jun 16, 2023
There're 3 layers in the RSC module graph: server → client → action. "Action" means that a Client Component re-enters the server layer by importing a file with `"use server"`, and it should behave the same as the server layer but you can't enter the client layer again (hence we have a 3rd layer name).

Since the action layer has the same behavior and module resolution rules, it should be bundled just like the server layer.

Closes #50658. Originally the issue was that `auth/next` isn't being bundled on the action layer, and it has the async local storage imported. Because of that, that storage comes from node_modules instead of the server bundle.
fix NEXT-1290
@shuding shuding reopened this Jun 16, 2023
@shuding shuding closed this as completed Jun 16, 2023
@shuding shuding closed this as completed Jun 16, 2023
@karlhorky
Copy link
Contributor

karlhorky commented Jun 20, 2023

Comment 1594357064: I guess this PR may also fix the error that is currently happening when importing a Server Action in a Client Component

Confirmed, next@13.4.7-canary.1 (from #51367) also fixes imports of Server Actions in Client Components 🎉 eg:

'use client';

import { logout } from './(auth)/logout/actions';

export function LogoutButton() {
  return (
    <form>
      <button formAction={logout}>
        logout
      </button>
    </form>
  );
}

Before

Uncaught Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.
    at Object.<anonymous> (/Users/k/projects/next-js-example-spring-2023-vienna-austria/node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/compiled/server-only/index.js:1:7)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at server-only (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/app/profile/[username]/page.js:352:18)
    at __webpack_require__ (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_action)/./database/connect.ts:5:69)
    at (sc_action)/./database/connect.ts (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/app/profile/[username]/page.js:2149:1)
    at __webpack_require__ (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_action)/./database/sessions.ts:10:66)
    at (sc_action)/./database/sessions.ts (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/app/profile/[username]/page.js:2171:1)
    at __webpack_require__ (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_action)/./app/(auth)/logout/actions.ts:8:76)
    at (sc_action)/./app/(auth)/logout/actions.ts (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/app/profile/[username]/page.js:2011:1)
    at Function.__webpack_require__ (/Users/k/projects/next-js-example-spring-2023-vienna-austria/.next/server/webpack-runtime.js:33:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async endpoint (webpack-internal:///(sc_action)/./node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/build/webpack/loaders/next-flight-action-entry-loader.js?actions=%5B%5B%22%2FUsers%2Fk%2Fprojects%2Fnext-js-example-spring-2023-vienna-austria%2Fapp%2F(auth)%2Flogout%2Factions.ts%22%2C%5B%22logout%22%5D%5D%5D&__client_imported__=true!:7:18)
    at async /Users/k/projects/next-js-example-spring-2023-vienna-austria/node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/server/app-render/action-handler.js:251:35
    at async handleAction (/Users/k/projects/next-js-example-spring-2023-vienna-austria/node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/server/app-render/action-handler.js:171:13)
    at async wrappedRender (/Users/k/projects/next-js-example-spring-2023-vienna-austria/node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/server/app-render/app-render.js:994:37)
    at async doRender (/Users/k/projects/next-js-example-spring-2023-vienna-austria/node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/server/base-server.js:1031:34)
    at async cacheEntry.responseCache.get.incrementalCache.incrementalCache (/Users/k/projects/next-js-example-spring-2023-vienna-austria/node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/server/base-server.js:1152:40)
    at async /Users/k/projects/next-js-example-spring-2023-vienna-austria/node_modules/.pnpm/next@13.4.4-canary.0_@babel+core@7.21.8_react-dom@18.2.0_react@18.2.0_sass@1.62.1/node_modules/next/dist/server/response-cache/index.js:99:36
Screenshot 2023-06-20 at 10 53 53

After

No error! 🎉


Thanks again @shuding 🙌

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants