# NextJS getSession() is returning null

**URL:** <https://community.auth0.com/t/nextjs-getsession-is-returning-null/76233>\
**Category:** Get Help\
**Tags:** nextjs, auth0\
**Created:** [January 12, 2022, 2:17am UTC](https://community.auth0.com/t/nextjs-getsession-is-returning-null/76233 "2022-01-12T02:17:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neo-Ciber94](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@Neo-Ciber94](https://community.auth0.com/u/Neo-Ciber94)\
**Post date:** [January 12, 2022, 2:17am UTC](https://community.auth0.com/t/nextjs-getsession-is-returning-null/76233/1 "2022-01-12T02:17:16Z")

</div>

I’m using NextJS 12 with Auth0 but when I try to access the **session** from an api/route is always null.

```plaintext
import { getSession } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const session = getSession(req, res);
  const user = session?.user;
  console.log("SESSION: ", { session }); // null
  
  res.json({ protected: "My Secret", id: user?.sub });
}

```

But when using **getSession()** from the **page** or **getServerSideProps** the session is returned corretly.

Is there something I’m missing?

---

<div class="post-metadata">

**Author:** ![moxie.dee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/moxie.dee/32/39957_2.png) [@moxie.dee](https://community.auth0.com/u/moxie.dee)\
**Post date:** [February 7, 2023, 10:23am UTC](https://community.auth0.com/t/nextjs-getsession-is-returning-null/76233/2 "2023-02-07T10:23:25Z")

</div>

Could this be because you didn’t wrap the handler around `withApiAuthRequired`?

---

<div class="post-metadata">

**Author:** ![kdesimini](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kdesimini/32/21577_2.png) [@kdesimini](https://community.auth0.com/u/kdesimini)\
**Post date:** [March 11, 2023, 5:46pm UTC](https://community.auth0.com/t/nextjs-getsession-is-returning-null/76233/3 "2023-03-11T17:46:32Z")

</div>

I’m also seeing this and I have the withApiAuthRequired

```auto
export default withApiAuthRequired(async function (req: any, res: any) {
  const { method } = req;

  const session = getSession(req, res);
  const user = session?.user;
  console.log(session.user)
...
  }
});

```

---

<div class="post-metadata">

**Author:** ![theitaliandev](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/theitaliandev/32/46110_2.png) [@theitaliandev](https://community.auth0.com/u/theitaliandev)\
**Post date:** [April 5, 2023, 9:29pm UTC](https://community.auth0.com/t/nextjs-getsession-is-returning-null/76233/4 "2023-04-05T21:29:16Z")

</div>

Since getSession() returns Promise\<default | null | undefined\> you need to wait for the promise to resolve or reject.

```auto
const session = await getSession(req, res)

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [April 19, 2023, 9:29pm UTC](https://community.auth0.com/t/nextjs-getsession-is-returning-null/76233/5 "2023-04-19T21:29:44Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
