Help with Middleware Session Cookie (Development vs Production)

Hey everyone! I’m running into an issue with my middleware, and I can’t quite figure out what’s causing it. The problem occurs when switching from the development environment to production. Here’s the scenario: In Development: The getSessionCookie function returns the session cookie as expected. In Production: After logging in, the session cookie is always null. Here’s the code I’m working with:
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth";

export async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);
console.log(sessionCookie);
// If the user is not logged in, redirect to the login page or '/'
if (!sessionCookie) {
return NextResponse.redirect(new URL("/auth/signin", request.url));
}
return NextResponse.next();
}

export const config = {
matcher: ["/private/:path*"], // The middleware will apply to all routes that start with '/private'
};
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth";

export async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);
console.log(sessionCookie);
// If the user is not logged in, redirect to the login page or '/'
if (!sessionCookie) {
return NextResponse.redirect(new URL("/auth/signin", request.url));
}
return NextResponse.next();
}

export const config = {
matcher: ["/private/:path*"], // The middleware will apply to all routes that start with '/private'
};
What I’ve tried so far: 1. Verified that cookies are being set correctly in production. 2. Checked that the environment variables and configurations match between dev and production. 3. Tried logging the sessionCookie to see if there's any difference, but it still comes up as null in production. Has anyone encountered this before or have any idea what might be causing this behavior?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?