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'],
};
2 Replies
bekacru
bekacru5d ago
try to see if there are cookies in the request object
LoLesttK
LoLesttK5d ago
try this
const cookie = request.cookies.get("better-auth.session_token")
const cookie = request.cookies.get("better-auth.session_token")

Did you find this page helpful?