oscar
oscar
BABetter Auth
Created by oscar on 3/15/2025 in #help
🛑 Next.js 15 – “cookies inside use cache” Error in Layout Component
Hey everyone, I’m running into an issue in my Next.js 15 app when trying to fetch session data inside my DashboardLayout component. I need to pass request headers to my getSession function, but I keep getting the following error about accessing dynamic data sources inside a cache scope. I’ve tried extracting the headers outside the function and passing them as arguments, but the issue persists. Has anyone encountered this before? Any ideas on how to properly handle this while keeping caching? Here’s my current code:
import { auth } from "@/lib/auth";
import { headers } from "next/headers";

async function getSession(sessionHeaders: Headers) {
"use cache";
const session = await auth.api.getSession({ headers: sessionHeaders });
return session;
}

export default async function DashboardLayout({
children,
}: Readonly<{ children: React.ReactNode }>) {
const sessionHeaders = await headers();
const session = await getSession(sessionHeaders);

return (
<main>
{children}
<pre>{JSON.stringify(session, null, 2)}</pre>
</main>
);
}
import { auth } from "@/lib/auth";
import { headers } from "next/headers";

async function getSession(sessionHeaders: Headers) {
"use cache";
const session = await auth.api.getSession({ headers: sessionHeaders });
return session;
}

export default async function DashboardLayout({
children,
}: Readonly<{ children: React.ReactNode }>) {
const sessionHeaders = await headers();
const session = await getSession(sessionHeaders);

return (
<main>
{children}
<pre>{JSON.stringify(session, null, 2)}</pre>
</main>
);
}
Here's the error:
⨯ Error: Route /app used "cookies" inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "cookies" outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache
at async getSession
4 | async function getSession(sessionHeaders: Headers) {
5 | "use cache";
> 6 | const session = await auth.api.getSession({ headers: sessionHeaders });
| ^
7 | return session;
8 | }
9 | {
environmentName: 'Cache',
digest: '1943676181'
}
⨯ Error: Route /app used "cookies" inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "cookies" outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache
at async getSession
4 | async function getSession(sessionHeaders: Headers) {
5 | "use cache";
> 6 | const session = await auth.api.getSession({ headers: sessionHeaders });
| ^
7 | return session;
8 | }
9 | {
environmentName: 'Cache',
digest: '1943676181'
}
13 replies
BABetter Auth
Created by oscar on 2/18/2025 in #help
ADMIN plugin not working
hey! i didn't want to open a gh issue just yet because maybe i'm doing something wrong, but i just integrated the admin plugin and found that nothing seems to be working for me. 1. signing up a new user doesn't set any default role value. 2. trying to impersonate (manually created users) or create user with auth-client gives 404 if someone could help me, i will greatly appreciate it. also lmk if you need any extra info. thanks ☺︎
6 replies