Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with CredentialsProvider in NextAuth.js when Changing Credential Fields #12385

Open
Prashant-S29 opened this issue Dec 15, 2024 · 0 comments
Labels
bug Something isn't working providers triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@Prashant-S29
Copy link

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 credentials object, everything works fine.
  • When I change the credentials object 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 my tsconfig.json file.
  • 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

  1. 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
  2. Navigate to the Configuration File
    Locate the config.ts file in the following project structure:

    root/
    └── src/
        └── server/
            └── auth/
                └── config.ts
    
  3. Observe the Error
    Open the config.ts file 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 credentials object?
  • Are there any additional configurations required in NextAuth.js to support custom credential fields?

Any help or suggestions would be greatly appreciated!

@Prashant-S29 Prashant-S29 added bug Something isn't working providers triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working providers triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

1 participant