Next.js Discord

Discord Forum

React Context is unavailable in server components, calling getServerSession?

Unanswered
Egyptian Mau posted this in #help-forum
Open in Discord
Egyptian MauOP
import { getServerSession } from "next-auth";
// import { NextUIProvider } from "@nextui-org/react";
import { SessionProvider } from "next-auth/react";
import { authConfig } from "./api/auth/[...nextauth]/route";

export async function Providers({ children }: { children: React.ReactNode }) {
  const session = await getServerSession(authConfig);
  return (
    <SessionProvider session={session}>
      {/* <NextUIProvider> */}
      {children}
      {/* </NextUIProvider> */}
    </SessionProvider>
  );
}
`

Why would this component cause this error? Isn't getServerSession supposed to be called in Server Components?

Error: React Context is unavailable in Server Components

8 Replies

Plott Hound
Did you put “use client” at the top of this file?
Egyptian MauOP
No I did not what you see is the full file
Plott Hound
Try adding it
Anything to do with react context needs to be in a file marked with “use client”
Remove any nextauth imports and consts, remove the session provider wrapper, mark file as “use client”, remove async from the function
You only need a session provider if you plan to access the session in client components. Otherwise you just call getServerSession in server components as you need it