JS_noob
JS_noob

Reputation: 39

Getting 400 response from Firebase Auth REST API

I'm learning React and trying to understand how to make signup with Firebase. Just can't find what am I doing wrong. Thank you all for any help. I'm using a request link from here: https://firebase.google.com/docs/reference/rest/auth?hl=cs

Here is a code of my request:

export const signInUser = () => {
  return async (dispatch) => {
    const sendSignInRequest = async () => {
      const response = await fetch(
        "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyDkDPtv28NKNnAxKYN_RFTG36lp3-IraAE",
        {
          method: "POST",
          body: JSON.stringify({
            email: "[email protected]",
            password: "12345TREWQ!asd",
            returnSecureToken: true,
          }),
          headers: {
            "Content-Type": "application/json",
          },
        }
      ).then((res) => {
        console.log(res);
        return res;
      });

      return response;
    };

    try {
      const respnseStatus = await sendSignInRequest();
    } catch (error) {
      console.log(error);
    }
  };
};

this is what I'm getting from the server:

Response {
    type: 'cors', 
    url: 'https://identitytoolkit.googleapis.com/v1/accounts…ignUp?key=AIzaSyDkDPtv28NKNnAxKYN_RFTG36lp3-IraAE', 
    redirected: false, 
    status: 400, 
    ok: false, 
…}

Thank you.

Upvotes: 1

Views: 9756

Answers (1)

karim elsaidy
karim elsaidy

Reputation: 1386

  • I think the error here that this email is already exist because you hard code it so it will be successful first attempt only and then it will be already exist you should check that if the response status give you error .

there are three types of errors maybe you will face in sign up with email and password in firebase

  • the first one email already exist
  • and second is TOO_MANY_ATTEMPTS_TRY_LATER
  • and the third is WEAK_PASSWORD : Password should be at least 6 characters, it's preferred to check that password at least 6 characters before sending it to the API so you always avoiding the third error.

-and by the way you don't need to use then here while you using async/await so your code will be like that

 export const signInUser = (email,password) => {
      return async (dispatch) => {
        const sendSignInRequest = async () => {
          const response = await fetch(
            "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyDkDPtv28NKNnAxKYN_RFTG36lp3-IraAE",
            {
              method: "POST",
              body: JSON.stringify({
                email: email
                password: password
                returnSecureToken: true,
              }),
              headers: {
                "Content-Type": "application/json",
              },
            }
          )
          const data =  await response.JSON();

         if (response.ok) {
            dispatch({type:'SIGN-UP'})
          } else if (!respnose.ok && data.error.message === "EMAIL_EXISTS"){
            console.log('this email is already exist')
          } else if(!respnose.ok&data.error.message ==="TOO_MANY_ATTEMPTS_TRY_LATER"){
            alert('TOO_MANY_ATTEMPTS_TRY_LATER')
          }
    
        };
    
        try {
          const respnseStatus = await sendSignInRequest();
  
        } catch (error) {
          console.log(error);
        }
}
};

Upvotes: 3

Related Questions