This is the function generating the error. It's a simple function to get the token from the session
4 |
5 | export async function getUserToken() {
> 6 | const session = await getServerSession(authOptions)
| ^
7 |
8 | return session?.user?.token
9 | }
I've been struggling for a couple days trying to figure out what is the issue. So far I have managed to isolate it to layout
& page
RSC that try to make us of next-auth
s getServerSession
. I need to be able to pass the session to make request on my backend and whenever I try to populate a client component with api calls that need the session the error is thrown.
Any insights or recommended workaround available?
Here is the main layout that needs the request to fill the TeamSwitcher
export default async function RootLayout({ children, params: { lng } }: Props) {
const clients = await getClients()
return (
<>
<div className="flex flex-col">
<div className="border-b">
<div className="flex h-16 items-center px-4">
{/* @ts-expect-error Server Component */}
<MainNav
lng={lng}
title={siteConfig.name}
items={siteConfig.mainNav}
/>
<div className="ml-auto flex items-center space-x-4">
<TeamSwitcher clients={clients} />
{/* @ts-expect-error Server Component */}
<UserNav lng={lng} user={user} />
</div>
</div>
</div>
{children}
</div>
</>
)
}
This is the api call, everything pretty straightforward
export async function getClients(): Promise<Client[]> {
const token = await getUserToken()
if (!token) throw Error("Missing token for request")
return await api
.headers({
accept: "application/json",
token,
})
.get("/clients")
.json()
}
getServerSession
) to perform redirections (index to sign-in if not signed, etc). also confused by the error overall, i've no idea why it breaks. – MisheargetStaticProps
but I'm still not sure I understand the issue. Seems to me that is related to static to dynamic rendering but I can't find any documentation that explains it very clearly – Virginavirginal