# getSession in next middleware randomly starts retuning null for users

**URL:** <https://community.auth0.com/t/getsession-in-next-middleware-randomly-starts-retuning-null-for-users/187213>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [May 26, 2025, 10:12pm UTC](https://community.auth0.com/t/getsession-in-next-middleware-randomly-starts-retuning-null-for-users/187213 "2025-05-26T22:12:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![faizansaleem13504](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/faizansaleem13504/32/74986_2.png) [@faizansaleem13504](https://community.auth0.com/u/faizansaleem13504)\
**Post date:** [May 26, 2025, 10:12pm UTC](https://community.auth0.com/t/getsession-in-next-middleware-randomly-starts-retuning-null-for-users/187213/1 "2025-05-26T22:12:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![teodor.andrei](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/teodor.andrei/32/75320_2.png) [@teodor.andrei](https://community.auth0.com/u/teodor.andrei)\
**Post date:** [May 27, 2025, 10:45am UTC](https://community.auth0.com/t/getsession-in-next-middleware-randomly-starts-retuning-null-for-users/187213/3 "2025-05-27T10:45:34Z")

</div>

Hi @faizansaleem13504, and welcome to the Auth0 Community!

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

Thanks!  
Teodor.

---

<div class="post-metadata">

**Author:** ![faizansaleem13504](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/faizansaleem13504/32/74986_2.png) [@faizansaleem13504](https://community.auth0.com/u/faizansaleem13504)\
**Post date:** [May 27, 2025, 12:19pm UTC](https://community.auth0.com/t/getsession-in-next-middleware-randomly-starts-retuning-null-for-users/187213/4 "2025-05-27T12:19:46Z")

</div>

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
