Skip to content

can not handle error from AuthError v-beta  #11747

@h-rajabi

Description

@h-rajabi

Environment

auth.config file

      id: "credentials",
      async authorize(credentials) {
        if (!credentials) {
          console.log("first");
          return null;
        }
        try {
          const response = await axiosInstance.post("/api/v1/auth", {
            phoneNumber: credentials.phone,
            password: credentials.password,
          });

          if (response.status === 200 && response.data) {
            return response.data;
          } else if (!response.data) {
            return null;
          } else {
            throw new Error(`Unexpected status code: ${response.status}`);
          }
        } catch (error: any) {
          if (error.response) {
            // Handle specific status codes
            switch (error.response.status) {
              case 400:
                throw new Error("Invalid credentials.");
              case 401:
                throw new Error(
                  "Unauthorized. Please check your username and password."
                );
              case 403:
                throw new Error("Access forbidden.");
              case 500:
                throw new Error("Server error. Please try again later.");
              default:
                throw new Error("An error occurred during login.");
            }
          } else if (error.request) {
            // No response was received from the server
            throw new Error(
              "No response from server. Please check your network connection."
            );
          } else {
            // Other errors
            throw new Error("Login failed due to an unexpected error.");
          }
        }

      },
    }),

my server action

  const validateData = UserLoginScema.safeParse(values);
  if (validateData.success) {
    const phone = validateData.data.userName;
    const password = validateData.data.password;
    try {
      const res=await signIn("credentials", {
        phone,
        password,
        redirect: false,
      });
      // console.log(res);
      
      return { statuscode: 200, message: "test success" };
    } catch (error) {
      if (error instanceof AuthError) {
        
        switch (error.type) {
          case "CredentialsSignin":
            return {
              statuscode: 400,
              message: "phoneNumber or password is not corect",
            };
          default:
            return { statuscode: 400, message: "something went wrong" };
        }
      }
      throw error;
    }
  }
  return { statuscode: 400, message: "invalid credentails" };
};```

### Reproduction URL

https://github.com/h-rajabi/test-credentials.git

### Describe the issue

in my log terminal server i get this error message 
```CredentialsSignin: Read more at https://errors.authjs.dev#credentialssignin
    at Module.callback (webpack-internal:///(action-browser)/./node_modules/@auth/core/lib/actions/callback/index.js:230:23)

and then i try to wen user can`t login throw new Error("some error)
or ``` throw new AuthError("some error")```
but this not work and get CallBackError

ervery thing is work but this message show in server and this not good

How to reproduce

.

Expected behavior

all error must handle and don`t show anything in my server log

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions