Search code examples
javascripttypescriptnext.jsnext-auth

"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.

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"