Auth0 not clearing the session after 400 series error occurs

Hello. I’m working on several nextjs projects, that has only one tenant in auth0. And, depending on project, users have different roles to login and cannot access to other web depending on role. and at that point error occurs. when user, whose role is not allowed to login, tries to login to website, it throws an error says access_denied, and somehow some session is saved and not deleted.

I tried logout but result is still the same. after the error occurs user is stuck in /api/auth/callback page and becomes not possible to login again.

if anyone had similar situation and got solved the issue, please let me know. :frowning:

Thank you.

what i did in auth0.js file is:

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

export default handleAuth({
    async callback(req, res) {
        try {
            await handleCallback(req, res);
        } catch (error) {
            console.log("code: ", error.code);
            console.log("name: ",;
            console.log("cause: ", error.cause.errorDescription);
            res.setHeader("Set-Cookie", "some-cookie=someValue; Max-Age=0");
            res.setHeader("Cache-Control", "no-store, must-revalidate");
            res.status(error.status || 500).end(error.message);