Getting logins_count from management api from Next.js API route (using pages router)


I’m trying to redirect users based on their login count but I’m having problems with JWT token expiration. The error message is “Expired token received for JSON Web Token validation”. However, when I try making the exact same request in Postman, it works.

My pages/api/auth/[...auth0].js file looks like this:

import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';

const afterCallback = (req, res, session, state) => {
    const uid = session.user.sub;

    var myHeaders = new Headers();
    myHeaders.append("Accept", "application/json");
    myHeaders.append("Authorization", "Bearer REDACTED");

    var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'

    fetch(`${uid}?fields=logins_count`, requestOptions)
        .then(response => response.json())
        .then(result => {
            if (result.logins_count > 1) {
                console.log('redirect to returning users');
            } else {
                console.log('redirect to onboarding');
        .catch(error => console.log('error', error));

    return session;

export default handleAuth({
    async callback(req, res) {
        try {
            await handleCallback(req, res, { afterCallback });
        } catch (error) {
            res.status(error.status || 500).end(error.message);

Hi @jeff14,

Welcome to the Auth0 Community!

In your request, is the token expired? You can decode it at and look at the payload to see if everything is looking correct.

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