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

Cannot return custom error to the client side in the Next-Auth v5 Credentials Auth

Open dsmabulage opened this issue 8 months ago • 7 comments

Provider type

Credentials

Environment

 "next": "14.2.3",
 "next-auth": "^5.0.0-beta.19",

Reproduction URL

https://github.com/dsmabulage/inventes

Describe the issue

I'm trying to implement the email, password login in the next app using next-auth v5.

In the NextAuth function inside authorize, I tried various methods to throw a custom error message that can passed to the user to show what's going wrong with the credentials.

How to reproduce

export const { auth, handlers, signIn, signOut } = NextAuth({
  session: {
    strategy: 'jwt',
  },
  pages: {
    signIn: '/auth/login',
  },
  providers: [
    CredentialsProvider({
      credentials: {
        email: {},
        password: {},
      },
      async authorize({ email, password }) {
        try {
          throw new Error(
            JSON.stringify({ errors: 'user.errors', status: false })
          );
        } catch (error) {
          throw new Error(
            JSON.stringify({ errors: ' catch user.errors', status: false })
          );
        }
      },
    }),
  ],

Expected behavior

I want to get that error message to the client side and show that error message to the user.

dsmabulage avatar Jun 17 '24 14:06 dsmabulage