Next.js Discord

Discord Forum

Next-auth validation errors.

Unanswered
Common carp posted this in #help-forum
Open in Discord
Common carpOP
I got credentials login set up, and it works but i want to show different error messages instead of one in different cases.
providers: [
        CredentialsProvider({
            name: 'credentials',
            credentials: {
                email: { label: 'Email', type: 'email' },
                password: { label: 'Password', type: 'password' },
            },
            async authorize(credentials) {
                try {
                    if (!credentials?.email || !credentials.password) {
                        throw new Error('All fields must be filled.')
                    }
                    const existingAccount = await prisma.account.findUnique({
                        where: { email: credentials?.email },
                    })
                    if (!existingAccount) {
                        throw new Error('Invalid email or password.')
                    }
                    if (!existingAccount.active) {
                        throw new Error('Account is not active. Please check your email for activation link.')
                    }
                    const passwordMatch = await compare(credentials.password, existingAccount.password)
                    if (!passwordMatch) {
                        throw new Error('Invalid email or password.')
                    }

                    return {
                        id: existingAccount.id,
                        name: existingAccount.name,
                        email: existingAccount.email,
                    }
                } catch (error: any) {
                    console.error('Authorization Error:', error.message)
                    throw error
                }
            },
        }),

This is my auth function, and below is submit form function:
    const onSubmit = async (values: z.infer<typeof FormSchema>) => {
        setIsLoading(true)
        try {
            await signIn('credentials', {
                email: values.email,
                password: values.password,
                redirect: false,
            })
            router.push('/dashboard')
        } catch (error: any) {
            setIsLoading(false)
            console.error(error.response)
        }
    }

So the problem is, that on the server side i got all the specified errors, but on client side there is only generic error
VM1673 loginForm.tsx:50 
       POST http://localhost:3000/api/auth/callback/credentials 401 (Unauthorized)


Which looks like that.

If anyone can show me how to get the errors on client i will be grateful. Thanks!

0 Replies