Auth0 NextJS - Refresh Sessions

Hello,

I’m using ReactJS with NextJS and the Auth0 SDK for my application. I’ve integrated <UseProvider> in my layout.tsx file, and the login, signup, and logout functionalities work perfectly.

I’ve also integrated the Stripe payment API to allow users to subscribe to the premium mode. After a successful payment, I call my own API using Auth0’s API Management to update the user’s role to “premium.”

However, I can’t figure out how to update the user’s session on the client side (ReactJS/NextJS) without logging them out, in order to give them access to certain premium resources in my app, etc, so i need to access rôle, i have already create a actions but its for post login…

Do you have any ideas or solutions to solve this issue?

Thanks you,
Dylan.

Hi @dylanbourbotte6,

Welcome to the Auth0 by Okta Community!

To fix this, you must obtain a refresh token. One way to do so is to set the refresh value equal to true. Take a look at the sample code below, along with the relevant documentation for more details.

Modify the session after refresh

// pages/api/my-handler.js
import { getAccessToken } from '@auth0/nextjs-auth0';

const afterRefresh = (req, res, session) => {
  session.user.customProperty = 'foo';
  delete session.idToken;
  return session;
};

export default async function MyHandler(req, res) {
  const accessToken = await getAccessToken(req, res, {
    refresh: true,
    afterRefresh,
  });
};

Hope this helps!

1 Like