getSession in next middleware randomly starts retuning null for users

getSession in next middleware randomly starts retuning null for users. As a result, user is navigated back to login page and after logging in, even then getSession doesn’t return valid user session.
I am using @auth0/nextjs-auth0 version 3.5.0

Hi @faizansaleem13504, and welcome to the Auth0 Community!

Can I please see the code snippet where you call getSession()?

Thanks!
Teodor.

Hi @teodor.andrei, I use it in the middleware. Here’s the code snippet
export default async function middleware(req: NextRequest) {

import { getSession } from ‘@auth0/nextjs-auth0/edge’;
const session = await getSession();

if (!session) {
  const loginUrl = new URL('/api/auth/login', req.url);
  loginUrl.searchParams.set('returnTo', req.url);
  return NextResponse.redirect(loginUrl);
}

The users get stuck in a loop, where from middleware they and redirected to /login page and when they return using returnTo url, they are again redirected to /login page even after successful login