-
-
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
getServerSession does not work in Next 13.4 Server Functions when they are imported in client components #7486
Comments
@balazsorban44 Sorry about the confusion! It will return but the server will throw an error (see screenshot) and the session is not there. |
temporary fix (for me): const req = { |
@balazsorban44 this issue still persists even w/ the |
Here is a workaround that you can use if you want to call import { cookies, headers } from 'next/headers';
import { getServerSession as originalGetServerSession } from 'next-auth';
export const getServerSession = async () => {
const req = {
headers: Object.fromEntries(headers() as Headers),
cookies: Object.fromEntries(
cookies()
.getAll()
.map((c) => [c.name, c.value]),
),
};
const res = { getHeader() {}, setCookie() {}, setHeader() {} };
const authOptions = { <insert auth options here> }
// @ts-ignore - The type used in next-auth for the req object doesn't match, but it still works
const session = await originalGetServerSession(req, res, authOptions);
return session;
}; |
This absolutely works. Have you created PR for this yet @ludwigbacklund ? |
I have not. In fact, I'm not sure why it even works as I basically just copied the original implementation from next-auth. Like someone mentioned, it might have to do with |
The problem persists with |
Thanks @ludwigbacklund 🙌 |
The workaround posted by @ludwigbacklund works - but is this going to be fixed? |
I'm not using the app directory. I'm on the pages directory performing SSR with getServerSideProps. The Session strategy is set to a database. Here is the Issue I created: #7752 |
thanks for the workaround @ludwigbacklund, much appreciated |
Thank you so much @ludwigbacklund This is my use case for getting login user from session info. We can get required data from NextReuqest.
|
In my case the getServerSection was returning always null.
I don`t know if this are the same issue as yours but just in case, i am posting it here. I will do some research to better understand the subject and i will come back to this issue. |
Environment
Reproduction URL
https://codesandbox.io/p/sandbox/awesome-johnson-o9i04r
Describe the issue
When using server actions in client components in the new Next 13.4 update,
getServerSession
fails withError: Invariant: Method expects to have requestAsyncStorage, none available
.Before the Next 13.4.2-canary.2 release the
cookies
andheaders
function fromnext/headers
failed so I thought this was the reason. However with the new release this should be fixed (see here, and try out in the reproduction repo).Calling cookies and headers now works. Interestingly enough, doing the same fails in next auth. The stack trace reveals that
the following lines in the next-auth package are responsible
Please note:
Both server actions work when called in server components. Only when importing in a client component the first one breaks.
How to reproduce
action.ts
and how they are invoked inpage.tsx
Expected behavior
The expected behaviour would be for getServerSession to work in server actions which are imported in client components.
The text was updated successfully, but these errors were encountered: