The user does not have an active session or is not authenticated

I’m using Next.js version 14.

<Link href="/api/auth/logout" className={childrenClassName}>
            <MdLogout size={16} />
            Logout
</Link>

This is my app/api/auth/[auth0]

import { handleAuth, handleCallback, handleLogin } from "@auth0/nextjs-auth0";
import { NextResponse } from "next/server";
import { NextApiRequest, NextApiResponse } from "next";

const isProd = process.env.NODE_ENV === "production";
const BASE_URL = isProd ? process.env.AUTH0_BASE_URL! : "http://localhost:3000";

export const GET = handleAuth({
  async login(req: NextApiRequest, res: NextApiResponse) {
    try {
      const url = new URL(req.url!);
      const searchParams = url.searchParams;
      const invitation = searchParams.get("invitation")!;
      const organization = searchParams.get("organization")!;

      if (!invitation) {
        NextResponse.json("Missing invite parameter", { status: 500 });
      }

      return await handleLogin(req, res, {
        authorizationParams: {
          invitation,
          organization,
        },
      });
    } catch (error) {
      return NextResponse.redirect(`${BASE_URL}/error=${error}`);
    }
  },
  async callback(req: NextApiRequest, res: NextApiResponse) {
    try {
      return await handleCallback(req, res, {});
    } catch (error) {
      return NextResponse.redirect(`${BASE_URL}?error=${error}`);
    }
  },
});

Whenever I try to log the user out, I get this error:

The user does not have an active session or is not authenticated.

Does anyone know how to fix this?

1 Like