Next.js Discord

Discord Forum

Build error in next-auth

Answered
Birman posted this in #help-forum
Open in Discord
BirmanOP
Why is this build error occuring when running npm-run-build?
In src/app/api/auth/[...nextauth]/route.ts
export const authOptions: NextAuthOptions = {
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {},
      async authorize(credentials, _req) {
        const URL = String(env.NEXT_PUBLIC_API_URL) + "/account/login/";
        const { email, password } = credentials as { email: string, password: string };

        const res = await loginUser(URL, { email, password });
        if (res.ok) return await res.json().data;        
        return null;
      }
    })
  ],
  callbacks: {
    async session({ session, token }: any) {
      session.access = token.access
      session.refresh = token.refresh

      if (session?.access ?? false) {
        const URL = String(env.NEXT_PUBLIC_API_URL) + "/account/me/"

        const res = await getUserDetail(URL, token.access);

        if (res.ok) {
           // logic
        }
      }

      return session
    },
    async jwt({ token, user }: any) {
      if (user) {
        // logic
      }

      if (!user) return refreshAccessToken(token)
      return token
    }
  },
  pages: { signIn: '/login' }
}

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

The error I got during build is attached as an image.
Can anyone help me solve this issue? Thanks for reading.
Answered by Ray
hi, try exporting the authOptions object to some where else and import it in src/app/api/auth/[...nextauth]/route.ts
View full answer

3 Replies

Answer
@Birman Thanks. This seems to solve the issue.
yep because next is expecting only route handler exports in route.ts