Why does my NextAuthJS discord login work in the test environment but cannot be built to production? (NextJS 13)

huangapple go评论77阅读模式
英文:

Why does my NextAuthJS discord login work in the test environment but cannot be built to production? (NextJS 13)

问题

我正在尝试将一个NextJS应用部署到Vercel。这是一个使用AuthJS的测试登录模型。当我使用npm run dev运行程序时,它运行正常,并且我能够登录到我的Discord帐户,该帐户已连接到开发者门户上的一个应用程序。但是,当我将它部署到Vercel并尝试构建用于生产的应用程序时,我收到了以下错误。

类型错误:路由“app/api/auth/[...nextauth]/route.ts”与Next.js路由所需的类型不匹配。
“authOptions”不是有效的路由导出字段。

为了上下文说明,我正在使用NextJS 13,并且它的应用程序文件夹方法来渲染页面,还使用了NextAuth.js。

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

export const authOptions: NextAuthOptions = {
  providers: [
    DiscordProvider({
      clientId: process.env.DISCORD_CLIENT_ID!,
      clientSecret: process.env.DISCORD_CLIENT_SECRET!,
    })
  ]
};

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST }

这是我在路由内找到的代码,相对路径为app/api/auth/[...nextauth]/route.ts,在运行程序时似乎没有任何问题,只有在尝试构建时才会出现问题。由于我是新手网站开发,之前没有遇到过这种问题,所以我不知道该怎么办,感到困惑。非常感谢任何帮助。

英文:

I am trying to deploy a NextJS application to vercel. This is a test sign in model using AuthJS. When I run the program using npm run devit works fine and I am able to log in to my discord account which is connected to an application on the developer portal. However, when I deploy it to Vercel and try and build the application for production I receive this error.

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

For context I am using NextJS 13 and it's app folder method of rendering pages along with the NextAuth.js.

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

export const authOptions: NextAuthOptions = {
  providers: [
    DiscordProvider({
      clientId: process.env.DISCORD_CLIENT_ID!,
      clientSecret: process.env.DISCORD_CLIENT_SECRET!,
    })
  ]
};

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST }

This is my code found inside the route, and the relative path to this folder is app/api/auth/[...nextauth]/route.ts which doesn't seem to have any problems when I run the program, only when trying to build it. I am not sure what to do as I am new to web development and have not encountered such an issue before and am perplexed. Any help is vastly appreciated.

答案1

得分: 0

我修复了问题 - 我只需要从export const authOptions中移除export即可。

英文:

I fixed the issues - all I needed to do was remove the export from export const authOptions

huangapple
  • 本文由 发表于 2023年5月30日 02:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定