Search code examples
authenticationnext.jsnext-auth

Route "[...nextauth]/route.ts" does not match the required types of a Next.js Route. Invalid configuration "GET":


I am getting a type error after migrating from V4 to V5 next-auth with my route.ts file, which is preventing my app from compiling and running and is located at: src/app/api/auth/[...nextauth]/route.ts

next: ^14.2.3 => 14.2.3 
next-auth: ^5.0.0-beta.17 => 5.0.0-beta.17 
react: 18.3.1 => 18.3.1

This is the error:

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.
  Invalid configuration "GET":

route.ts

import NextAuth from "next-auth";
import { authOptions } from "../../../config/authOptions";

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

authOptions.ts

import { NextAuthConfig } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export const authOptions: NextAuthConfig = {
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: { label: "Email", type: "text", placeholder: "Enter Email" },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        if (!credentials || !credentials.email || !credentials.password) {
          return null;
        }
        const user = users.find((item) => item.email === credentials.email);
        if (user?.password === credentials.password) {
          const modifiedUser = {
            ...user,
            accountStatus: "Active",
          };

          return modifiedUser;
        }
        return null;
      },
    }),
  ],
  pages: {
     signIn: "/signin",
     error: "/autherror",
  },
};

Build error


Solution

  • Moving your config to another directory (i moved it to root level) and doing this inside src/app/api/auth/[...nextauth]/route.ts:
    import NextAuth from 'next-auth' import authOptions from '../../../../lib/configs/auth/authOptions'

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

    Fixed this: https://github.com/nextauthjs/next-auth/issues/10845