代码之家  ›  专栏  ›  技术社区  ›  charan vendra

“auth”不是有效的路由导出字段

  •  1
  • charan vendra  · 技术社区  · 1 年前

    我正在使用 Next.js v14 下一个auth v5测试版 .
    我有这个在我的 route.tsx 放置在 app/api/auth/[...nextauth]/ 目录

    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,
        })
      ]
    })
    

    当部署到azure Web应用程序时,我在构建过程中遇到了以下错误 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.
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   Ahmed Abdelbaset    1 年前

    Next.js希望您从其保留文件(例如route.js、page.js等)导出特定导出。

    在您的情况下, route.ts 应该只出口 GET , POST , PUT , PATCH , DELETE , OPTIONS other Segment Config Options 。但是,你出口了 auth , signIn signOut 虽然

    例如,这些应该在一个单独的文件中 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,
        })
      ]
    })
    

    然后,在您的 app/auth/[...nextauth]/route.ts ,

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