Anup
Anup
BABetter Auth
Created by Anup on 2/27/2025 in #help
Cookies not working in next js middleware
Hi guys, why cookies value is being return null in next js middleware but the value exists in the browser's cookies
import { NextRequest, NextResponse } from 'next/server';
import { getSessionCookie } from 'better-auth';

export async function middleware(request: NextRequest) {
const cookies = getSessionCookie(request);
console.log({ cookies });
if (!cookies) {
return NextResponse.redirect(new URL('/', request.url));
}
return NextResponse.next();
}

export const config = {
matcher: ['/dashboard'],
};
import { NextRequest, NextResponse } from 'next/server';
import { getSessionCookie } from 'better-auth';

export async function middleware(request: NextRequest) {
const cookies = getSessionCookie(request);
console.log({ cookies });
if (!cookies) {
return NextResponse.redirect(new URL('/', request.url));
}
return NextResponse.next();
}

export const config = {
matcher: ['/dashboard'],
};
3 replies