nextauthjs / nextauthjs/next-auth
Issue with CredentialsProvider in NextAuth.js when Changing Credential Fields
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 28.4k
- Forks
- 4k
- PR merge metrics
- No merged PRs in 30d
Description
Provider type
Credentials
Environment
System:
OS: Windows 11 10.0.22631
CPU: (12) x64 AMD Ryzen 5 5625U with Radeon Graphics
Memory: 2.27 GB / 15.34 GB
Binaries:
Node: 22.7.0 - C:\Program Files\nodejs\node.EXE
npm: 10.8.3 - C:\Program Files\nodejs\npm.CMD
bun: 1.1.27 - ~\AppData\Roaming\npm\bun.CMD
Browsers:
Edge: Chromium (127.0.2651.74)
Internet Explorer: 11.0.22621.3527
npmPackages:
@auth/prisma-adapter: ^2.7.2 => 2.7.4
next: ^15.0.1 => 15.0.3
next-auth: 5.0.0-beta.25 => 5.0.0-beta.25
react: ^18.3.1 => 18.3.1
Reproduction URL
https://github.com/Prashant-S29/ici-fest-skit.git
Describe the issue
Issue with CredentialsProvider in NextAuth.js when Changing Credential Fields
Description
I am experiencing an issue with CredentialsProvider in NextAuth.js. The error occurs when I modify the default credentials object to include additional fields.
Here is the situation:
- When using the default
credentialsobject, everything works fine. - When I change the
credentialsobject to include additional fields, I encounter an error.
I have strict: true set in my tsconfig.json file.
Working Code
This configuration works without any issues:
CredentialsProvider({
type: "credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
});
Non-Working Code
When I change the credentials object as shown below, I get an error:
CredentialsProvider({
type: "credentials",
credentials: {
adminId: { label: "Admin ID", type: "text" },
password: { label: "Password", type: "password" },
eventId: { label: "Event ID", type: "text" },
},
});
TS Error message
When I change the credentials object as shown above, I get this TS error:
Type '(credentials: Credential | null) => Promise<{ id: string; role: string; eventId?: undefined; } | { id: string; eventId: string; role?: undefined; } | null>' is not assignable to type '(credentials: Partial<Record<"eventId" | "adminId" | "password", unknown>>, request: Request) => Awaitable<User | null>'.
Types of parameters 'credentials' and 'credentials' are incompatible.
Type 'Partial<Record<"eventId" | "adminId" | "password", unknown>>' is not assignable to type 'Credential'.
Types of property 'adminId' are incompatible.
Type 'unknown' is not assignable to type 'string | undefined'.ts(2322)
credentials.d.ts(53, 5): The expected type comes from property 'authorize' which is declared here on type 'Partial<CredentialsConfig<{ adminId: { label: string; type: string; }; password: { label: string; type: string; }; eventId: { label: string; type: string; }; }>>'
Additional Context
- I have strict type checking enabled (
strict: true) in mytsconfig.jsonfile. - The error message does not provide a clear solution.
- It seems like there might be a type mismatch or an issue with how the new fields are being validated.
How to reproduce
Steps to Reproduce
Prerequisites
- Ensure you have Node.js and npm installed
- Have Git configured on your system
Steps
-
Clone the Repository and Set Up the Project
Clone the repository and install the required dependencies by following the version specifications mentioned in the project documentation.git clone <repo-url> cd <repo-folder> # Install dependencies npm install -
Navigate to the Configuration File
Locate theconfig.tsfile in the following project structure:root/ └── src/ └── server/ └── auth/ └── config.ts -
Observe the Error
Open theconfig.tsfile and review the error message that appears. This might be related to incorrect configurations or other issues in the file.
Expected behavior
Expected Behavior
I expect to be able to modify the credentials object to include custom fields without encountering any errors.
Actual Behavior
An error occurs when I add custom fields to the credentials object.
Environment
- NextAuth.js Version: [Specify version]
- TypeScript Version: [Specify version]
- Node.js Version: [Specify version]
- Operating System: [Specify OS]
Question
- Is there a specific TypeScript type I need to define to allow custom fields in the
credentialsobject? - Are there any additional configurations required in NextAuth.js to support custom credential fields?
Any help or suggestions would be greatly appreciated!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/server/auth/config.ts in the reproduction repository and reproduce the strict TypeScript error. Then inspect credentials.d.ts at the reported line 53 and the CredentialsProvider configuration around the authorize callback. Done means the custom adminId, password, and eventId fields type-check without the reported parameter incompatibility.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nextjs, nodejs, typescript
- Domain
- authentication
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100