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:
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);
}
}
});