1

Auth0 provides a routing setup kit for the traditional Pages Router. Is there a way to use Auth0 routes on the new App Router?

Here is an example code by Auth0 doc:

// pages/api/auth/[...auth0].js
import { handleAuth } from '@auth0/nextjs-auth0';

export default handleAuth();

If you put the code at src/app/api/auth/[...auth0]/route.js, which is a way of App Router, Next.js shows following errors:

- error Detected default export in '/…/app/api/auth/[...auth0]/route.ts'. Export a named export for each HTTP method instead.
- error No HTTP methods exported in '/…/app/api/auth/[...auth0]/route.ts'. Export a named export for each HTTP method.

Because the App Router requires a different interface from the express.js way. Here is an example for the new way:

// src/app/api/auth/[...auth0]/route.ts
import { NextResponse } from "next/server";

export async function GET() {
  return NextResponse.json({ message: "Hello, World!" });
}

I'm looking for an escape hatch that works until they officially support the App Router.

Ginpei
  • 2,958
  • 5
  • 15
  • 25

1 Answers1

0

Even in a project that is using App Router, legacy “pages/api” directory works.

https://community.auth0.com/t/auth0-does-not-work-with-next-js-app-router/106656/5

So I just needed to put the router code at pages/api/auth/[...auth0].js beside my src/, not under.

Ginpei
  • 2,958
  • 5
  • 15
  • 25