# RefreshToken is undefined in NextJS

**URL:** <https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [November 7, 2023, 11:05pm UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444 "2023-11-07T23:05:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oimtiaz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/oimtiaz/32/51251_2.png) [@oimtiaz](https://community.auth0.com/u/oimtiaz)\
**Post date:** [November 7, 2023, 11:05pm UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444/1 "2023-11-07T23:05:34Z")

</div>

Hello,

I am using the nextjs-auth0 library to authenticate users in my app. I need refresh tokens for my Auth0 Application, however I am not getting them. I have included my `initAuth0` below, which includes the scopes etc I need back. I have confirmed that my application has a grant type of “Refresh Token” enabled, and that absolute expiration is enabled.

`utils/auth_utils.ts`:

```plaintext
	baseURL:
		process.env.AUTH0_BASE_URL ||
		"https://" + process.env.NEXT_PUBLIC_VERCEL_URL,
	issuerBaseURL: process.env.AUTH0_ISSUER_BASE_URL,
	clientID: process.env.AUTH0_CLIENT_ID,
	clientSecret: process.env.AUTH0_CLIENT_SECRET,
	secret: process.env.AUTH0_SECRET,
	authorizationParams: {
		response_type: "code",
		scope: "openid profile email offline_access",
	},
	session: {
		rolling: true,
		rollingDuration: 60 * 60 * 24,
		absoluteDuration: 60 * 60 * 24 * 7,
	},
});

```

The response I get back has the lines:

```auto
  accessTokenExpiresAt: 1699484513,
  refreshToken: undefined,

```

When I change the scopes in the above code, for example removing `profile`, it is changed in the returned session.

The way I am checking for the session is:

```plaintext
	req: NextApiRequest,
	res: NextApiResponse,
	session: Session,
) => {
	console.log(session);
	return session;
};
export default auth0.handleAuth({
	async callback(req: NextApiRequest, res: NextApiResponse) {
		try {
			await auth0.handleCallback(req, res, { afterCallback });
		} catch (error: unknown) {
			const err = error as { status?: number; message?: string };
			res.status(err.status || 500).end(err.message);
		}
	},
});

```

Any guidance would be much appreciated!

---

<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:** [November 8, 2023, 12:29am UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444/3 "2023-11-08T00:29:52Z")

</div>

Hey @oimtiaz !

I’m not seeing an [audience](https://community.auth0.com/t/what-is-the-audience/71414) param anywhere, but it looks like you’re receiving an access token so you may be handling that elsewhere. Can you confirm that whichever API identifier you are using as your `audience` while initializing the SDK has the “[Allow Offline Access](https://auth0.com/docs/get-started/apis/api-settings#access-settings)” option toggled in the API settings in your dashboard?

---

<div class="post-metadata">

**Author:** ![oimtiaz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/oimtiaz/32/51251_2.png) [@oimtiaz](https://community.auth0.com/u/oimtiaz)\
**Post date:** [November 8, 2023, 12:46am UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444/4 "2023-11-08T00:46:39Z")

</div>

Hi!

So, I’m not using an API, but my Auth0 Application itself. When I manually go through the authorization/authentication flow using the Application’s information, I am getting a refresh\_token back. When I run through the same flow in my NextJS app, I’m seeing that I don’t get the refresh\_token back. I’m very confused why it would work in Postman but not in Next. My Application has refresh token rotation enabled, and the absolute expiration is on (with no option to turn it off).

---

<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:** [November 8, 2023, 1:12am UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444/5 "2023-11-08T01:12:43Z")

</div>

Thanks for sharing!

> [@oimtiaz](#):
>
> So, I’m not using an API, but my Auth0 Application itself.

The API I am referencing is the API for which the access token is intended to use against.

> [@oimtiaz](#):
>
> When I manually go through the authorization/authentication flow using the Application’s information, I am getting a refresh\_token back.

Do you mind sharing how you are going about this manually/in postman? Please redact any sensitive information.

---

<div class="post-metadata">

**Author:** ![oimtiaz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/oimtiaz/32/51251_2.png) [@oimtiaz](https://community.auth0.com/u/oimtiaz)\
**Post date:** [November 8, 2023, 5:42am UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444/6 "2023-11-08T05:42:04Z")

</div>

I actually realized I did not need a refresh token, so I have moved away from this approach. Thank you for your help!

---

<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:** [November 9, 2023, 12:23am UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444/7 "2023-11-09T00:23:14Z")

</div>

No problem! Happy to help 🙂

---

<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:** [November 23, 2023, 12:24am UTC](https://community.auth0.com/t/refreshtoken-is-undefined-in-nextjs/120444/8 "2023-11-23T00:24:06Z")

</div>

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