TypeScript error for the Credentials provider
Description 🐜
The changes to Providers in [...nextauth].ts in V4 appered a TypeScript error for the Credentials provider.
Type 'CredentialsConfig<{ username: { label: string; type: string; }; password: { label: string; type: string; }; }>' is not assignable to type 'Provider'.
Type 'CredentialsConfig<{ username: { label: string; type: string; }; password: { label: string; type: string; }; }>' is not assignable to type 'CredentialsConfig<{}>'.
Types of property 'authorize' are incompatible.
Type '(credentials: Record<"username" | "password", string>, req: NextApiRequest) => Awaitable<Omit<User, "id"> | { id?: string | undefined; } | null>' is not assignable to type '(credentials: Record<never, string>, req: NextApiRequest) => Awaitable<Omit<User, "id"> | { id?: string | undefined; } | null>'.
Types of parameters 'credentials' and 'credentials' are incompatible.
Type 'Record<never, string>' is missing the following properties from type 'Record<"username" | "password", string>': username, password
Is this a bug in your own project?
No
How to reproduce ☕️
import bcrypt from 'bcryptjs';
import prisma from 'lib/prisma';
import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
export default NextAuth({
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
username: { label: 'Username', type: 'text' },
password: { label: 'Password', type: 'password' }
},
async authorize(credentials: any, req) {
const user = await prisma.user.findUnique({
where: {
username: credentials.username
}
});
if (!user) {
return null;
}
const valid = await bcrypt.compare(credentials.password, user.password);
if (!valid) {
console.log(`Credentials not valid`);
return null;
}
if (user) {
return { ...user, email: user.username };
}
return null;
}
})
]
});
Screenshots / Logs 📽
No response
Environment 🖥
System: OS: Windows 10 10.0.19042 CPU: (8) x64 Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz Memory: 3.44 GB / 11.81 GB Binaries: Node: 14.15.4 - D:\Node\node.EXE npm: 6.14.10 - D:\Node\npm.CMD Browsers: Edge: Spartan (44.19041.1023.0), Chromium (93.0.961.38) Internet Explorer: 11.0.19041.906 npmPackages: next: ^10.2.3 => 10.2.3 next-auth: ^4.0.0-beta.2 => 4.0.0-beta.2 react: ^17.0.2 => 17.0.2
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
This is a follow-up issue from #2677.
Unfortunately, I still cannot reproduce it, so it's probably a misconfiguration somewhere.
Make sure/double-check that your node_modules/next-auth/package.json contains at least version 4.0.0-beta.2.
Here is how it looks locally for me. Note, I also removed the any, as it wasn't needed:

(Small suggestion, if you don't use req, just don't declare it.)
You could try creating a CodeSandbox or a reproduction repo if you still think it is an error, but I have a strong feeling it is something with the version being incorrect in node_modeules.
I'll close this, for now, but if you can prove the error in a reproducible way, I will gladly reopen it.
@balazsorban44 Can you please show how you're importing the provider?
I literally pasted the code from above. 😅
You ask me for a reproduction of a working example? 😄 I'll create one, fine.
Haha, I swear I must be doing something dumb.

Here it is: https://stackblitz.com/edit/nextjs-hdxwhs?file=pages/api/auth/[...nextauth].ts
Make sure you actually use beta.2!
Here's a reproducible issue: https://codesandbox.io/s/charming-feynman-0p5gf?file=/src/pages/api/auth/%5B...nextauth%5D.ts
Now that just doesn't make sense to my brain... 👀
Me too man, me too 😂
Maybe a difference in the tsconfig.json causing this?
I have to go now, but please keep digging :pray:
Update: it's the strict flag. next-auth is being developed with strict: false.
If I turn it to true, other stuff might break too... (counted 62 errors when I ran tsc) This might be a bigger issue then. For now, you could set strict: false, and the error will go away. I'll have to investigate this further and decide what we should do. :thinking:
The issue is when the tsconfig.json has strict mode turned on: "strict":true,
Sweet man, thank you for all your time working with me on this. For now I'll set strict to false, but would be awesome to support strict mode too :) But totally get if that would be a ballache.
I will close this in favour of #2709.
The current fix for this issue is to turn off strict mode in tsconfig.json.
As an update to this just in case anyone else comes across the problem, upgrading from 4.0.0-beta.4 to 4.0.0-beta.7 fixed this issue. It actually created another though lol.
it did not, see my answer here https://github.com/nextauthjs/next-auth/issues/2709#issuecomment-973183324
I'm facing something similar it seems. Asked here at SO https://stackoverflow.com/questions/70990262/how-to-write-authorize-function-correctly-in-typescript-for-nextauthjs
Nextjs with its 4,555,332,233 issue. Keep it up Vercel!
does anyone have a fix for this? im also getting this error.
Bumping this, still an issue as of May 2023. My workaround was to simply type the return as any:
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Username", type: "text", placeholder: "[email protected]" },
password: { label: "Password", type: "password", placeholder: "verysecurepassword" },
},
async authorize (credentials) {
try {
if (!credentials) throw new Error("no credentials to log in as");
const { email, password } = credentials;
const {
salt,
password: hashedPassword,
...user
} = await client.user.findUniqueOrThrow({
where: {
email,
},
});
const logged = await comparePassword(password, salt, hashedPassword);
if (!logged) throw new Error("invalid credentials");
return user as any; // fix for https://github.com/nextauthjs/next-auth/issues/2701
} catch (ignored) {
return null;
}
}
})
Can anyone tell me what's wrong with my code i have tried everything even the ( "strict":false ) but it still doesn't work
` CredentialsProvider({
name: "Credentials",
async authorize(credentials, req) {
if (!credentials) {
throw new Error("Credentials are required");
}
const isUserExists = await User.findOne({ email: credentials.email });
if (!isUserExists) {
throw new Error("No user found")
}
const checkPassword = await bcryptjs.compare(
credentials.password,
isUserExists.password
);
if (!checkPassword || isUserExists.email !== credentials.email) {
throw new Error("Invalid credentials")
}
return isUserExists
},
})`
Can anyone tell me what's wrong with my code i have tried everything even the ( "strict":false ) but it still doesn't work
` CredentialsProvider({ name: "Credentials", async authorize(credentials, req) { if (!credentials) { throw new Error("Credentials are required"); } const isUserExists = await User.findOne({ email: credentials.email }); if (!isUserExists) { throw new Error("No user found") } const checkPassword = await bcryptjs.compare( credentials.password, isUserExists.password ); if (!checkPassword || isUserExists.email !== credentials.email) { throw new Error("Invalid credentials") } return isUserExists }, })`
For one you forgot the credentials field in the provider, in my comment that's just above yours you've got a working example with prisma too.