# Next.js invalid token using middleware, which token to use?

**URL:** <https://community.auth0.com/t/next-js-invalid-token-using-middleware-which-token-to-use/109085>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [June 12, 2023, 4:57pm UTC](https://community.auth0.com/t/next-js-invalid-token-using-middleware-which-token-to-use/109085 "2023-06-12T16:57:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![claim727](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/claim727/32/47751_2.png) [@claim727](https://community.auth0.com/u/claim727)\
**Post date:** [June 12, 2023, 4:57pm UTC](https://community.auth0.com/t/next-js-invalid-token-using-middleware-which-token-to-use/109085/1 "2023-06-12T16:57:28Z")

</div>

I’m using the code below to add an auth header to a backend .net core api. On the .net core side I’m getting an unauthorized 401 error using the accessToken. If I change it to IdToken from the getProfile call I get invalid audience. Which token should I use and how to track down why the auth check is failing?

```auto
export async function middleware(req: NextRequest) {
 
  const res = NextResponse.next();
  const user = await getSession(req, res);

  if (user) {
    if (user.accessTokenExpiresAt && user.accessTokenExpiresAt * 1000 < Date.now()) {
      return NextResponse.redirect('/api/auth/login')
    } 
    const requestHeaders = new Headers(req.headers);
    requestHeaders.set('Authorization', `Bearer ${user.accessToken}`);
    const response = NextResponse.next({ request: { headers: requestHeaders } });
    return response;
  }
  else{
    return NextResponse.redirect('/api/auth/login')
  }

  console.log(res.headers);
  return res;
};

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 12, 2023, 11:47pm UTC](https://community.auth0.com/t/next-js-invalid-token-using-middleware-which-token-to-use/109085/2 "2023-06-12T23:47:26Z")

</div>

Hey there @claim727 !

> [@claim727](#):
>
> Which token should I use and how to track down why the auth check is failing?

You should be using the access token against your backend API - Can you confirm the audience of the access token matches that of the API identifier of your API as defined in your Auth0 dashboard? Is your API middleware checking for scopes or anything else or just verifying the access token? A missing audience is a misstep here:  
[https://community.auth0.com/t/what-is-the-audience/71414](https://community.auth0.com/t/what-is-the-audience/71414)

[https://community.auth0.com/t/why-is-my-access-token-not-a-jwt-opaque-token/31028](https://community.auth0.com/t/why-is-my-access-token-not-a-jwt-opaque-token/31028)

Let us know if this is not the case!

---

<div class="post-metadata">

**Author:** ![claim727](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/claim727/32/47751_2.png) [@claim727](https://community.auth0.com/u/claim727)\
**Post date:** [June 13, 2023, 8:12pm UTC](https://community.auth0.com/t/next-js-invalid-token-using-middleware-which-token-to-use/109085/3 "2023-06-13T20:12:05Z")

</div>

Hi tyf,

Appreciate the help! So the setup is as follows. I have a next.js front end and a .net core backend. The next.js setup was done following the auth0 next.js guide, and there’s no mention of setting up an audience config in that guide.

1. Can confirm on the .net backend the audience matches the one on the dashboard. -.net config screenshot [2023-06-13\_13-05-34.png](https://www.screencast.com/t/NzGS9neoIHA) -dashboard screenshot [2023-06-13\_13-07-52.png](https://www.screencast.com/t/wl7LxYd3XDe)
2. No checking of scopes, just checking access token.

One thing to note is if I paste the user.accessToken into this site [https://jwt.io/](https://jwt.io/), I get an invalid token error [2023-06-13\_13-10-26.png](https://www.screencast.com/t/LlRjI3tWGs)

If I paste the user.IdToken it checks out valid… so why would that be? Here’s an example of the access token ‘eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIiwiaXNzIjoiaHR0cHM6Ly9kZXYtc3c2MmxnNGE1cHVnc3ZtZy51cy5hdXRoMC5jb20vIn0…ypBsUKEiPtLuMm0E.bUvttx6EtALPkGWwMeuWupSFRUC2j8kgDbcrVD5GL7sfqtKLK-Q71lnt-Enw1lw2-rkV4uLVJzgqmsXjjWFvTA8boItW7VzM9mvboawkKirq2zEmQNz\_wPbHeysp0\_uMR12iCHs7FFoDYykWmjCO\_iMO3DWqNwq4q\_AD2iqcvfQQDBSx0ymw-PVUcnBFuAeDzV5-yJ\_qS8knyfwo3IS6ufEYuEfhrX8jEpbplLMlsAspnNWL9CMNnZcsCxEpEzNXXqklwvHGnuredRSirsUJX4ty22IFhC8296nWLkXzGv66kMDt0ttRICQFqvX7TtVWDM9xSPDupAdLuLdxF23\_yYt4ulQaVJc.-EEbiuZhXK6PPEWlok7DyA’

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 27, 2023, 11:49pm UTC](https://community.auth0.com/t/next-js-invalid-token-using-middleware-which-token-to-use/109085/4 "2023-10-27T23:49:14Z")

</div>


