next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

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

Open avi312singh opened this issue 9 months ago • 1 comments

Environment

  System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M2 Pro
    Memory: 239.02 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.5.0 - ~/.nvm/versions/node/v21.5.0/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v21.5.0/bin/npm
  Browsers:
    Chrome: 124.0.6367.119
    Safari: 17.4.1
  npmPackages:
    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 

Reproduction URL

https://github.com/avi312NHS/next-auth-issue

Describe the issue

After migrating to next-auth v5 I am getting a build error with my current route.ts configuration

image

How to reproduce

As per reproduction repo cloned from - https://github.com/nextauthjs/next-auth-example, export auth configuration options to another directory and try to npm run build

app/api/auth/[...nextauth]/route.ts

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

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

config/authOptions.ts

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

let users: any[] = [];

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',
  },
};

Expected behavior

No TS build failures after migrating from V4 to v5, I cannot see any docs related to the changing of the route exporting format -> Next auth docs This code was fully functional with:

"next": "^13.5.6",
"next-auth": "^4.24.5"

Other unanswered issues with same error

https://stackoverflow.com/questions/78214384/type-error-route-app-api-auth-nextauth-route-ts-does-not-match-the-requi https://stackoverflow.com/questions/77637651/authoptions-is-not-a-valid-route-export-field

avi312singh avatar May 07 '24 14:05 avi312singh

Using that last stackoverflow link actually solved the error for me in my app.

mattixpet avatar May 08 '24 11:05 mattixpet

Yes likewise

avi312singh avatar May 11 '24 14:05 avi312singh

correct

Minttsaka avatar May 13 '24 03:05 Minttsaka

To fix the issue, simply remove the export keyword from the line export const authOptions. The code should look like this: const authOptions.

bernabedev avatar Sep 04 '24 13:09 bernabedev

Worth noting that the docs show code exporting authOptions like this in https://next-auth.js.org/configuration/nextjs#getserversession , that's how i ran into this issue

image

davidchang avatar Oct 08 '24 17:10 davidchang