© 2026 Hedgehog Software, LLC
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
import { getSessionCookie } from "better-auth/cookies";
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"}`); }
▲ Next.js 15.2.4 (Turbopack) - Local: http://localhost:3000 - Network: http://192.168.1.4:3000 - Environments: .env