Currently, I am working on a project using Next.js 14 and attempting to configure NextAuth with the app/router. Unfortunately, I have encountered a type error that is proving difficult to resolve.
Below is my route.ts file:
// ./app/api/[...nextauth]/route.js
import NextAuth from "next-auth/next";
import CredentialsProvider from "next-auth/providers/credentials";
import bcrypt from "bcrypt";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient;
export const authOptions = {
adapter: PrismaAdapter(prisma),
providers: [
CredentialsProvider({
name: "credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
// check to see if email and password are valid
if(!credentials || !credentials.email || !credentials.password)
return null;
// Check if the user exists
const user = await prisma.user.findUnique({
where: {
email: credentials.email
}
});
if(!user)
return null;
// Check if passwords match (if user exists)
const passwordsMatch = user.hashedPassword && await bcrypt.compare(credentials.password, user.hashedPassword);
if(!passwordsMatch)
return null;
// Return user object if everything is valid
return user;
}
})
],
session: {
strategy: 'jwt' as const // Explicitly type as 'jwt'
},
secret: process.env.NEXTAUTH_SECRET,
debug: process.env.NODE_ENV === "development",
};
export const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
This results in the following error:
Argument of type '{ adapter: Adapter; providers: CredentialsConfig<{ email: { label: string; type: string; }; password: { label: string; type: string; }; }>[]; session: { strategy: "jwt"; }; secret: string | undefined; debug: boolean; }' is not assignable to parameter of type 'AuthOptions'.
The types of 'adapter.createUser' are incompatible between these types.
Type '((user: AdapterUser) => Awaitable<AdapterUser>) | undefined' is not assignable to type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined'.
Type '(user: AdapterUser) => Awaitable<AdapterUser>' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
Types of parameters 'user' and 'user' are incompatible.
Property 'id' is missing in type 'Omit<AdapterUser, "id">' but required in type 'AdapterUser'.
I have attempted to change
export const authOptions = {
to
export const authOptions: AuthOptions = {
However, this change results in the error:
Type 'import("c:/<path>@auth/core/adapters").Adapter' is not assignable to type 'import("c:/<path>/node_modules/next-auth/adapters").Adapter'.
Types of property 'createUser' are incompatible.
Type '((user: AdapterUser) => Awaitable<AdapterUser>) | undefined' is not assignable to type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined'.
Type '(user: AdapterUser) => Awaitable<AdapterUser>' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
Types of parameters 'user' and 'user' are incompatible.
Property 'id' is missing in type 'Omit<AdapterUser, "id">' but required in type 'AdapterUser'.
I am struggling to understand what I am missing or doing wrong. Any assistance would be greatly appreciated.