Next.js Discord

Discord Forum

Complex Type Error After Adding `authorize` Function to Credentials Provider

Unanswered
Jersey Wooly posted this in #help-forum
Open in Discord
Jersey WoolyOP
I'm following the official Dashboard tutorial and am busy with Chapter 15: Adding Authentication. I made it to the step Adding the Credentials provider, and my code in auth.ts looked like this:
import NextAuth from 'next-auth';
import { authConfig } from './auth.config';
import Credentials from 'next-auth/providers/credentials';

export const { auth, signIn, signOut } = NextAuth({
    ...authConfig,
    providers: [Credentials({})],
});

Then in the step, Adding the sign in functionality, we are told to add an authorize function to the Credentials provider, and copying from the example make my code look like this;
import NextAuth from 'next-auth';
import { authConfig } from './auth.config';
import Credentials from 'next-auth/providers/credentials';
import { z } from 'zod';

export const { auth, signIn, signOut } = NextAuth({
    ...authConfig,
    providers: [Credentials({
        async authorize(credentials) {
            const parsedCredentials = z
                .object({ email: z.string().email(), password: z.string().min(6) })
                .safeParse(credentials);
        },
    })],
});

I'm using VSCode, and it puts a red swiggly under the name authorize, with the error text that starts like this:
Type '(credentials: Partial<Record<string, unknown>>) => Promise<void>' is not assignable to type '(credentials: Partial<Record<string, unknown>>, request: Request) => Awaitable<User | null>'.
  Type 'Promise<void>' is not assignable to type 'Awaitable<User | null>'.
...
credentials.d.ts(53, 5): The expected type comes from property 'authorize' which is declared here on type 'Partial<CredentialsConfig<Record<string, CredentialInput>>>'  

Being very new to TypeScript I can't make head or tail of this message, and could use some help. Maybe we don't have to decode the error message and somebody knows why pasting in the async authorize(credentials) function causes an error.

2 Replies

the authorize function has a 2nd param called req
and it expects the function to return a User instance