[NextAuth] 오류: "authOptions" is not a valid Route export field

엔케이·2024년 12월 16일

에러 내용

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

error Command failed with exit code 1.

원인

authOptionshandler를 함께 export해서 발생하는 문제
yarn dev 에서는 문제가 발생 안하지만 yarn build시 에러가 발생

import NextAuth from 'next-auth'
import GoogleProvider from 'next-auth/providers/google'

export const authOptions = {
  // Configure one or more authentication providers
  providers: [
    GoogleProvider({
      clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID ?? '',
      clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET ?? '',
    }),
  ],
}

const handler = NextAuth(authOptions)

export const GET = handler
export const POST = handler

해결방법

authOptionshandler를 파일을 분리해서 export 진행

// 파일 위치
// src/app/api/auth/[...nextauth]/options.ts

import GoogleProvider from 'next-auth/providers/google'

export const authOptions = {
  // Configure one or more authentication providers
  providers: [
    GoogleProvider({
      clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID ?? '',
      clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET ?? '',
    }),
  ],
}
// 파일 위치
// src/app/api/auth/[...nextauth]/route.ts

import NextAuth from 'next-auth'
import { authOptions } from './options'

const handler = NextAuth(authOptions)

export const GET = handler
export const POST = handler
profile
FE 개발자

0개의 댓글