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

TypeScript complains on `CredentialsConfig`

Open balazsorban44 opened this issue 5 years ago • 11 comments

Discussed in https://github.com/nextauthjs/next-auth/discussions/2672

Originally posted by Rhym September 6, 2021 Good morning! I am updating my next auth in preparation for the 4.0 release and my previously working Credentials provider has an issue with the authorize callback.

I receive a

"Types of parameters 'credentials' and 'credentials' are incompatible. Type 'Record<never, string>' is missing the following properties from type '{ username: string; password: string; }': username, password". "The expected type comes from property 'authorize' which is declared here on type 'Partial<CredentialsConfig<{}>>'"

I see that the CredentialsConfig takes a generic, but I'm not actually sure where to set the config. Any help would be appreciated, thank you. Example of my code:

providers: [
  // https://next-auth.js.org/configuration/providers
  CredentialsProvider({
    id: "login",
    name: "Login",
    credentials: {
      username: {},
      password: {},
    },

    authorize: async (credentials: { username: string; password: string }) => {
      const DATA = { username: credentials.username, password: credentials.password };
      // Login logic...
    },
  }),
],

balazsorban44 avatar Sep 06 '21 09:09 balazsorban44

The problem is in this file:

https://github.com/nextauthjs/next-auth/blob/beta/src/providers/credentials.ts

PRs welcome, otherwise I'll dig into it once I have the time.

balazsorban44 avatar Sep 06 '21 09:09 balazsorban44

Hi @Rhym

So the general thinking is that whatever fields you define on credentials will be available in the authorize callback, like so:

There was a small issue with the type I think, which I now corrected in #2683.

This all means that you don't actually need a generic!

balazsorban44 avatar Sep 06 '21 21:09 balazsorban44

Awesome, thank you @balazsorban44 for your swift response. So will that be available in BETA 2

Rhym avatar Sep 06 '21 22:09 Rhym

Yes, hoping to release it soon. Currently awaiting #2655

balazsorban44 avatar Sep 07 '21 07:09 balazsorban44

Oh, that's a nice feature. Looking forward to the release. Have a great day!

Rhym avatar Sep 07 '21 07:09 Rhym

4.0.0-beta.2 has been released, including a fix for this!

balazsorban44 avatar Sep 07 '21 09:09 balazsorban44

Hey, @balazsorban44 I've updated to beta.2 and now I get a fun new message with the following code:

providers: [
  Credentials({
    name: "Credentials",
    credentials: {
      username: {
        label: "username",
        type: "text",
      },
      password: {
        label: "password",
        type: "password",
      },
    },
    async authorize(credentials, _req) {
      // Auth logic...
    },
  }),
],

Typescript error:

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

Looks like the update to the config had a side effect?

Rhym avatar Sep 08 '21 03:09 Rhym

I cannot seem to reproduce: 1. 2.

If you still have issues, please open a new issue with a reproduction. :wink:

balazsorban44 avatar Sep 08 '21 07:09 balazsorban44

I have the same Typescript error:

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

dragoncorf avatar Sep 08 '21 15:09 dragoncorf

then please provide a reproduction. 😊 as mentioned, I cannot reproduce it

balazsorban44 avatar Sep 08 '21 15:09 balazsorban44

Yes! I made this issue with a reproduction #2701

dragoncorf avatar Sep 08 '21 16:09 dragoncorf