# How to get IdToken when authenticating in next.js

**URL:** <https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [January 27, 2024, 1:05am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835 "2024-01-27T01:05:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon20](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/simon20/32/48816_2.png) [@simon20](https://community.auth0.com/u/simon20)\
**Post date:** [January 27, 2024, 1:05am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/1 "2024-01-27T01:05:06Z")

</div>

Hello, everyone.

I am trying to integrate with Auth0 in next.js but encountered one problem.

After logging in Auth0 successfully, I want to get IdToken to send to our backend.

If anybody has idea or experience in it, looking forward to your help.

Thanks,

---

<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:** [January 27, 2024, 1:42am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/3 "2024-01-27T01:42:15Z")

</div>

Hey @simon20 welcome to the community!

> [@simon20](#):
>
> After logging in Auth0 successfully, I want to get IdToken to send to our backend.

Regardless of tech stack, you never want to use an ID token against a backend - You’ll need to utilize an access token instead. The following blog post does a great job clarifying the difference:

> **[ID Token and Access Token: What Is the Difference?](https://auth0.com/blog/id-token-access-token-what-is-the-difference/)**
>
> Learn what ID and access tokens are and how to correctly use them in the OpenID Connect and OAuth context.

In terms of next.js, the following examples should help:

> <https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#protect-an-api-route>

---

<div class="post-metadata">

**Author:** ![simon20](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/simon20/32/48816_2.png) [@simon20](https://community.auth0.com/u/simon20)\
**Post date:** [January 27, 2024, 2:06am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/4 "2024-01-27T02:06:49Z")

</div>

> [@ty.frith](#):
>
> you never want to use an ID token against a backend - You’ll need to utilize an access token instead. The following blog post does a great job clarifying the difference:

but from the Auth0 docs, the accessToken doesn’t contain userinfo.

---

<div class="post-metadata">

**Author:** ![Patricio879](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/patricio879/32/53826_2.png) [@Patricio879](https://community.auth0.com/u/Patricio879)\
**Post date:** [January 27, 2024, 8:11am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/5 "2024-01-27T08:11:33Z")

</div>

Thank you for sharing this information…

---

<div class="post-metadata">

**Author:** ![thameera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thameera/32/13430_2.png) [@thameera](https://community.auth0.com/u/thameera)\
**Post date:** [January 28, 2024, 1:43am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/6 "2024-01-28T01:43:26Z")

</div>

Note that if you are using the Auth0 Next.js SDK, you don’t need to specifically get ID or Access token to call the backend. If the user is logged in, the SDK will take care of this by using the session cookie. If you are using the App Router, the user info can be obtained in an API route with this code without having direct access to a token:

```javascript
const { user } = await getSession();

```

However, if the backend is not your Next.js app (eg: an externally hosted API), you can get the Access Token as described here: [https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route)

---

<div class="post-metadata">

**Author:** ![simon20](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/simon20/32/48816_2.png) [@simon20](https://community.auth0.com/u/simon20)\
**Post date:** [January 28, 2024, 2:15pm UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/7 "2024-01-28T14:15:45Z")

</div>

Hi @thameera, thanks for your help.  
When I wanna integrate with my own backend(not Next.js) which token is needed, accessToken or idToken?  
then, is there any way to get idToken in my client side?  
Thanks again.

---

<div class="post-metadata">

**Author:** ![thameera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thameera/32/13430_2.png) [@thameera](https://community.auth0.com/u/thameera)\
**Post date:** [January 28, 2024, 9:55pm UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/8 "2024-01-28T21:55:58Z")

</div>

No problem. You will need an Access Token to call any external API.

Generally you would not need to access the ID token by itself since it’s used for initial authentication only. When you get the user’s data from `getSession()`, the info you get is actually what was present in the ID token.

---

<div class="post-metadata">

**Author:** ![simon20](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/simon20/32/48816_2.png) [@simon20](https://community.auth0.com/u/simon20)\
**Post date:** [January 29, 2024, 12:07pm UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/9 "2024-01-29T12:07:41Z")

</div>

Thanks again for your help.  
I managed to get accessToken and used it for authorization in my rust backend.  
but it is gotten in client side(using react query).  
Is there a way to get it in getServersideProps and pass it as a parameter to component?  
To be honest, I tried it but it is only possible after loggin in so it occurs error before logging in.

---

<div class="post-metadata">

**Author:** ![thameera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thameera/32/13430_2.png) [@thameera](https://community.auth0.com/u/thameera)\
**Post date:** [January 29, 2024, 10:46pm UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/10 "2024-01-29T22:46:24Z")

</div>

You should be able to get the Access Token from getServersideProps as shown here: [Call external API from inside getServerSideProps and get the access token? · Issue #928 · auth0/nextjs-auth0 · GitHub](https://github.com/auth0/nextjs-auth0/issues/928#issuecomment-1333891254)  
The relevant code snippet is:

```javascript
export async function getServerSideProps(context) {
  const { accessToken } = await getAccessToken(context.req, context.res);
  // do something with access token
}

```

The token will not be available prior to login, so that scenario will need to be handled within getServersideProps.

---

<div class="post-metadata">

**Author:** ![simon20](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/simon20/32/48816_2.png) [@simon20](https://community.auth0.com/u/simon20)\
**Post date:** [January 30, 2024, 1:14am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/11 "2024-01-30T01:14:23Z")

</div>

Hi, @thameera

I am really happy for your care.

it worked after logging in. but once i click logout button, the session becomes invaild.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/4/0/40afa21bf8a90fe6d58e5da3712bc5a85fb7860f.png)

---

<div class="post-metadata">

**Author:** ![thameera](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thameera/32/13430_2.png) [@thameera](https://community.auth0.com/u/thameera)\
**Post date:** [January 30, 2024, 1:28am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/12 "2024-01-30T01:28:55Z")

</div>

No problem, @simon20.

What you are seeing is expected, right? The session will go away once the user logs out and the code should not be able to get the access token afterwards. You should ideally do that with a try/catch and handle the scenario when the user attempts to access the page while not logged in.

---

<div class="post-metadata">

**Author:** ![simon20](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/simon20/32/48816_2.png) [@simon20](https://community.auth0.com/u/simon20)\
**Post date:** [January 30, 2024, 3:16am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/13 "2024-01-30T03:16:09Z")

</div>

> [@thameera](#):
>
> you are seeing is expected, right? The session will go away once the user logs in and the code should not be able to get the access token afterwards. You should ideally do that with a try/catch and handle the scenario when the user attempts to access the page while not logged

thanks for your help.  
I’ve resolved it.  
Then, I am struggling against another issue… 🥵  
I need to implement `change password` functionality.  
I have one button-`change password` on my frontend.  
and once I click it, then need to redirect auth0-change password page.  
If you have any idea, I will really appreciate.  
Thanks.

---

<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:** [February 13, 2024, 3:16am UTC](https://community.auth0.com/t/how-to-get-idtoken-when-authenticating-in-next-js/125835/14 "2024-02-13T03:16:27Z")

</div>

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