# getSession unable to authenticate user in NextJS server-side

**URL:** <https://community.auth0.com/t/getsession-unable-to-authenticate-user-in-nextjs-server-side/121394>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, nextjs-auth0\
**Created:** [November 22, 2023, 2:25am UTC](https://community.auth0.com/t/getsession-unable-to-authenticate-user-in-nextjs-server-side/121394 "2023-11-22T02:25:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![keegan.r.s21](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/keegan.r.s21/32/52637_2.png) [@keegan.r.s21](https://community.auth0.com/u/keegan.r.s21)\
**Post date:** [November 22, 2023, 2:25am UTC](https://community.auth0.com/t/getsession-unable-to-authenticate-user-in-nextjs-server-side/121394/1 "2023-11-22T02:25:33Z")

</div>

I’m trying to protect a data request that’s made in the api. My basic setup is this:

In src/app/myroute/[id]/page.tsx:

```auto
import { getSession } from "@auth0/nextjs-auth0/edge";

export default async function page(props: Props) {
    const session = await getSession();
    const { id } = props.params;
    const url = `${process.env.BASE_URL}/api/myroute/${id}`;
    const req = new NextRequest(url);
    const res = await fetch(req);

```

Which sends a request to src/app/api/myroute/[id]/route.ts:

```auto
import { getSession } from "@auth0/nextjs-auth0/edge";

export async function GET(req: NextRequest, props: Props): Promise<Response> {
    const res = NextResponse.next();
    const session = await getSession(req, res);

    if (!session) {
        return new Response("Not logged in", { status: 401 });
    }

```

My issue is that getSession() always returns undefined regardless of the user’s authentication status on the front end.

I have tried everything that I can think of and scoured the forums but can’t see anything that suggests this should be an issue. I’ve also tried manually adding the accessToken cookie to the request object:

```auto
const req = new NextRequest(url, { headers: { cookie: session.accessToken as string } });

```

Any help is much appreciated!

---

<div class="post-metadata">

**Author:** ![SaqibHussain](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/saqibhussain/32/42924_2.png) [@SaqibHussain](https://community.auth0.com/u/SaqibHussain)\
**Post date:** [November 29, 2023, 8:48pm UTC](https://community.auth0.com/t/getsession-unable-to-authenticate-user-in-nextjs-server-side/121394/2 "2023-11-29T20:48:35Z")

</div>

Hi @keegan.r.s21

Welcome to the Auth0 Community.

I would suggest you check the Github repo examples like this one just an an example of protecting an API route [https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#protect-an-api-route](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#protect-an-api-route) as this may help you to spot what’s missing.

If you’re still having trouble you could post an issue here to get feedback directly from the SDK maintainers [https://github.com/auth0/nextjs-auth0/issues](https://github.com/auth0/nextjs-auth0/issues)

Warm regards.

---

<div class="post-metadata">

**Author:** ![keegan.r.s21](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/keegan.r.s21/32/52637_2.png) [@keegan.r.s21](https://community.auth0.com/u/keegan.r.s21)\
**Post date:** [December 6, 2023, 3:33am UTC](https://community.auth0.com/t/getsession-unable-to-authenticate-user-in-nextjs-server-side/121394/3 "2023-12-06T03:33:53Z")

</div>

Thanks for getting back to me @SaqibHussain.

This solved the issue! I’m not sure why my implementation didn’t work though… It seems that making a request with the function wrapped in withPageAuthRequired() makes it work.

---

<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:** [December 20, 2023, 3:34am UTC](https://community.auth0.com/t/getsession-unable-to-authenticate-user-in-nextjs-server-side/121394/4 "2023-12-20T03:34:08Z")

</div>

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