Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"auth" is not a valid Route export field

I am using Next.js v14 and next-auth v5 beta.
I have this in my route.tsx placed in app/api/auth/[...nextauth]/ directory:

import NextAuth from "next-auth";
import AzureADProvider from "next-auth/providers/azure-ad";

export const {
    handlers: { GET, POST },
    auth,
    signIn,
    signOut,
} = NextAuth({
  providers: [
    AzureADProvider({
      clientId: process.env.AZURE_AD_CLIENT_ID,
      clientSecret: process.env.AZURE_AD_CLIENT_SECRET,
      tenantId: process.env.AZURE_AD_TENANT_ID,
    })
  ]
})

When deploying to azure webapp, I get this error in the build process which is next build:

app/api/auth/[...nextauth]/route.tsx
Type error: Route "app/api/auth/[...nextauth]/route.tsx" does not match the required types of a Next.js Route.
  "auth" is not a valid Route export field.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Next.js expects you to export specific exports from it’s reserved file (e.g. route.js, page.jsx, etc…).

In your case, route.ts is supposed to only export GET, POST, PUT, PATCH, DELETE, OPTIONS, and other Segment Config Options. But, you exported auth, signIn, and signOut though.

These should be in a seperate file, for example auth.ts.

// src/auth.ts

import NextAuth from "next-auth";
import AzureADProvider from "next-auth/providers/azure-ad";

export const {
    handlers: { GET, POST },
    auth,
    signIn,
    signOut,
} = NextAuth({
  providers: [
    AzureADProvider({
      clientId: process.env.AZURE_AD_CLIENT_ID,
      clientSecret: process.env.AZURE_AD_CLIENT_SECRET,
      tenantId: process.env.AZURE_AD_TENANT_ID,
    })
  ]
})

Then, in your app/auth/[...nextauth]/route.ts,

export { GET, POST } from "../path/to/auth.ts"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading