Session cookies issue

In my next.js app console also it's showing session as
null
I wantto protect dashboard page with middleware.js . I see I can easily navigate to dashboard page, on console.log of sessionCookie from getSessionCookie it's showing sessionCookie id I also check devtools about the same Dev tools -> Application -> Cookies -> localhost:3000 -> better-auth with expiry set to March 3, 2025 but showing session as null on console. The middlware is working fine on incognito mode. Please tell what am I doing wrong 😭

This my code

middleware.js
import { NextResponse } from "next/server"; import { getSessionCookie } from "better-auth"; export async function middleware(request) { const sessionCookie = getSessionCookie(request); console.log("sessionCookie", sessionCookie); // Optionally pass config as the second argument if cookie name or prefix is customized. if (!sessionCookie) { return NextResponse.redirect(new URL("/", request.url)); } return NextResponse.next(); } export const config = { matcher: ["/dashboard"], // Specify the routes the middleware applies to };

lib/auth.js
import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql" }), session: { expiresIn: 60 * 60 * 24 * 7, // 7 days updateAge: 60 * 60 * 24, // 1 day (every 1 day the session expiration is updated) cookieCache: { enabled: true, maxAge: 5 * 60 // Cache duration in seconds } }, socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }, }, })
Was this page helpful?