【问题标题】:Getting 400 response from Firebase Auth REST API从 Firebase Auth REST API 获得 400 响应
【发布时间】:2021-10-21 01:12:39
【问题描述】:

我正在学习 React,并试图了解如何使用 Firebase 进行注册。只是找不到我做错了什么。谢谢大家的帮助。我正在使用来自这里的请求链接:https://firebase.google.com/docs/reference/rest/auth?hl=cs

这是我的请求代码:

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: "QWERTY12345@GMAIL.COM",
            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);
    }
  };
};

这是我从服务器得到的:

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

谢谢。

【问题讨论】:

    标签: reactjs redux react-redux firebase-authentication


    【解决方案1】:
    • 我认为这里的错误是这封电子邮件已经存在,因为您对其进行了硬编码,因此它只会在第一次尝试时成功,然后它就已经存在,您应该检查响应状态是否给您错误。

    在firebase中使用电子邮件和密码注册时可能会遇到三种类型的错误

    • 第一封电子邮件已经存在
    • 第二个是 TOO_MANY_ATTEMPTS_TRY_LATER
    • 第三个是 WEAK_PASSWORD :密码应至少为 6 个字符, 最好先检查密码至少 6 个字符 将其发送到 API,这样您就可以始终避免第三个错误。

    -顺便说一下,当您使用 async/await 时,您不需要在此处使用 then,因此您的代码将是这样的

     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);
            }
    }
    };
    

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 2017-10-30
      • 2015-08-18
      • 2020-08-29
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多