# Getting logins\_count from management api from Next.js API route (using pages router)

**URL:** <https://community.auth0.com/t/getting-logins-count-from-management-api-from-next-js-api-route-using-pages-router/115908>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [September 11, 2023, 1:45pm UTC](https://community.auth0.com/t/getting-logins-count-from-management-api-from-next-js-api-route-using-pages-router/115908 "2023-09-11T13:45:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff14](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jeff14/32/49749_2.png) [@jeff14](https://community.auth0.com/u/jeff14)\
**Post date:** [September 11, 2023, 1:45pm UTC](https://community.auth0.com/t/getting-logins-count-from-management-api-from-next-js-api-route-using-pages-router/115908/1 "2023-09-11T13:45:03Z")

</div>

Hello,

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:

```auto
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(`https://REDACTED.us.auth0.com/api/v2/users/${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);
        }
    }
});

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 15, 2023, 4:31pm UTC](https://community.auth0.com/t/getting-logins-count-from-management-api-from-next-js-api-route-using-pages-router/115908/3 "2023-09-15T16:31:00Z")

</div>

Hi @jeff14,

Welcome to the Auth0 Community!

In your request, is the token expired? You can decode it at [jwt.io](http://jwt.io) and look at the payload to see if everything is looking correct.

---

<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:** [September 29, 2023, 4:31pm UTC](https://community.auth0.com/t/getting-logins-count-from-management-api-from-next-js-api-route-using-pages-router/115908/4 "2023-09-29T16:31:13Z")

</div>

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