getSessionCookie() always null | Next.js 15.2

In my middleware I am using getSessionCookie() to check if a user has a session cookie. My middleware looks like this (shortend):
...
const sessionCookie = getSessionCookie(request);
if (!sessionCookie) {
return NextResponse.redirect(new URL(`/${locale}/auth/sign-in${callbackUrl}`, request.url));
}
...
...
const sessionCookie = getSessionCookie(request);
if (!sessionCookie) {
return NextResponse.redirect(new URL(`/${locale}/auth/sign-in${callbackUrl}`, request.url));
}
...
When using npm run dev I can access the protected routes only if I am logged in and everything works just as expected. If using npm run start after a build, `getSessionCookie()`` is always returning null and I cannot access the protected routes even when I am logged in and the session cookie is set. Any help is really appreciated. Thanks!
Solution:
The issue happens cause the getSessionCookie helper is expecting __secure prefixed cookie but the server is still setting non secure cookie cause it's running on http. You can pass useSecureCookie:false in get session cookie config. Or update to 1.2.5-beta.3 which sohuld fix the issue.
Jump to solution
2 Replies
Solution
bekacru
bekacru2mo ago
The issue happens cause the getSessionCookie helper is expecting __secure prefixed cookie but the server is still setting non secure cookie cause it's running on http. You can pass useSecureCookie:false in get session cookie config. Or update to 1.2.5-beta.3 which sohuld fix the issue.
Valu
ValuOP2mo ago
Thanks!

Did you find this page helpful?