Next.js Discord

Discord Forum

Returning server's message instead of Nextauth's messages

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I have a problem in Nextauth


....
        phone: { label: "Phone Number", type: "text" },
        otp: { label: "OTP", type: "text" },
      },
      authorize: async (credentials) => {
        if (!credentials) throw new Error('لطفا دوباره تلاش کنید');
        
        const { phone, otp } = credentials;

        try {
          const res = await fetch(`${API_BASE_URL}/auth/verify`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ phone, otp_code: otp }),
          });

          const textResponse = await res.text();

          if (!res.ok) throw new Error(textResponse || 'Unknown error occurred');

          return { id: phone as string, message: textResponse };
        } catch (error) {
          throw new Error(error instanceof Error ? error.message : 'An unexpected error occurred');
        }
      }
    })
  ],
});



I want to return the api's response instead of Nextauth's default errors, Like when we enter an incorrect code, the server responds with a message saying otp incorrect, but here when I try to set this I get some weird error and I couldn't figure out what should I do here :

[auth][error] CallbackRouteError: Read more at https://errors.authjs.dev#callbackrouteerror
[auth][cause]: Error: otp incorrect
....
[auth][details]: {
  "provider": "credentials"
}

I get this otp incorrect in return so this means it sends a request to server but it can't validate the rest

I'd appreciate any points or tips about what to do

3 Replies

Asiatic LionOP
Btw the server responds like this :
{
  "result": false,
  "messages": "otp incorrect"
}
I want this "messages" to be set in setError inside the login page
Asiatic LionOP