getSessionCookie does not work in middleware

I was trying to use getSessionCookie just now with my middleware and i kept getting null.

const handleAuth = async (request: NextRequest) => {
  // log all cookies
  console.log("FROM MIDDLEWARE");
  console.log(request.cookies);
  
  const sessionCookie = await getSessionCookie(request, {
    // Optionally pass config if cookie name or prefix is customized in auth config.
    cookieName: "session_token",
    cookiePrefix: "ft-auth",
    path: "/"
  }) as string | undefined;
  if (!sessionCookie) {
    console.log("FROM MIDDLEWARE");
    console.log("No session cookie found");
    return NextResponse.redirect(new URL("/login", request.url));
  }

  return NextResponse.next();
}

getSessionCookie is imported in the above snippet via import { getSessionCookie } from "better-auth/cookies";

i replaced it with a super simple stand-in util like this for now:
  export async function getSessionCookie(request: NextRequest, config?: { cookieName: string, cookiePrefix: string, path: string }) {
    // implement middleware logic
    const cookieStore = await import("next/headers").then(mod => mod.cookies());
    return cookieStore.get(`${config?.cookiePrefix || "ft-auth"}.${config?.cookieName || "session_token"}`);
  }



it works. but i hope yall find and fix the issue.

more info on the env:
  ▲ Next.js 15.2.4 (Turbopack)
   - Local:        http://localhost:3000
   - Network:      http://192.168.1.4:3000
   - Environments: .env
Was this page helpful?