Next.js 13.4和NextAuth类型错误:“AuthOptions”不可分配给类型“never”

rnmwe5a2  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(191)

我目前正在做一个Next.js 13.4项目,并试图使用应用程序/路由器设置NextAuth。但是,我遇到了一个类型错误,似乎无法解决。
下面是我的route.ts文件:

import NextAuth, { AuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

export const authOptions: AuthOptions = {
  providers: [
    DiscordProvider({
      clientId: process.env.CLIENT_ID as string,
      clientSecret: process.env.CLIENT_SECRET as string,
    }),
  ],
  session: {
    strategy: "jwt",
  },
  secret: process.env.NEXTAUTH_SECRET,
}

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST }

以下是运行'npm run build'时的错误消息:

- info Linting and checking validity of types ...Failed to compile.

.next/types/app/api/auth/[...nextauth]/route.ts:8:13
Type error: Type 'OmitWithTag<typeof import("C:/Users/Luk/Documents/Workspace/zerotwo-dash/src/app/api/auth/[...nextauth]/route"), "GET" | "POST" | "HEAD" | "OPTIONS" | "PUT" | "DELETE" | "PATCH" | "config" | ... 6 more ... | "runtime", "">' does not satisfy the constraint '{ [x: string]: never; }'.
  Property 'authOptions' is incompatible with index signature.
    Type 'AuthOptions' is not assignable to type 'never'.

   6 |
   7 | // Check that the entry is a valid entry
>  8 | checkFields<Diff<{
     |             ^
   9 |   GET?: Function
  10 |   HEAD?: Function
  11 |   OPTIONS?: Function

我真的不知道这里发生了什么...当我在nextauth的github页面上查找“AuthOptions”时,我发现我的代码没有任何问题。
我将感谢任何关于如何解决这个问题的见解或建议。先谢谢你了!

fiei3ece

fiei3ece1#

好吧,我自己解决了。对于有同样问题的人,我创建了一个新文件@/utils/authOptions.ts:

import { NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

export const authOptions: NextAuthOptions = {
    providers: [
        DiscordProvider({
            clientId: process.env.CLIENT_ID as string,
            clientSecret: process.env.CLIENT_SECRET as string,
        }),
    ],
    session: {
        strategy: "jwt",
    },
    secret: process.env.NEXTAUTH_SECRET,
}

并在@/API/auth/[... nextauth]/route.ts中使用它:

import { authOptions } from "@/utils/authOptions";
import NextAuth from "next-auth/next";

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

我改变了一点进口(进口NextAuth从“下一个认证/下一个”)我不知道为什么这工程tbh,但它确实...甚至改变了我的路线周围的进口。ts,因为它是以前不能解决它。如果像这样分开…

相关问题