Firebase 加载资源失败:服务器响应状态为 400 错误

zb2*_*b22 6 firebase firebase-authentication

我正在使用 Firebase 在网络平台上注册一个新用户,注册效果很好,我可以在 Firebase 控制台中看到新用户。

但是,当我在注册过程中检查一些错误(例如重复邮件)时,我在开发工具控制台中收到错误消息:

www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=.. iucensg Failed to load resource: the server responded with a status of 400 ()

这是我的代码,希望对您有所帮助:

 firebase.auth().createUserWithEmailAndPassword($scope.email, $scope.password).then((user) => {

            firebase.auth().currentUser.getToken(/* forceRefresh */ true).then(function (idToken) {

                var providerID = user.providerData["0"].providerId;

                $http({
                    method: 'POST',
                    url: url + '/userReg',
                    data: {
                        firstName: $scope.firstName,
                        lastName: $scope.lastName,
                        providerID
                    },
                    headers: {'x-auth': idToken},
                }).then(function successCallback(response) {

                    if (response.status == 200) {
                        // $scope.$emit('logoutEvent', true);
                        $sessionStorage.put('ActiveUser', {accToken: idToken, providerID: 'email'});
                        $scope.$parent.logoutShow = true;
                        $scope.$parent.diaryShow = true;
                        $scope.$parent.userShow = true;
                        $scope.$parent.signInShow = false;
                        $scope.$parent.registerShow = false;
                        $window.location.href = '#/diary';
                    }

                }, function errorCallback(response) {
                    console.log(response.data);
                });

            }).catch(function (error) {
                console.log(error);
            });

        }).catch(function (error) {
            $scope.alertShow = true;
            $scope.errorMessage = error.message;
        })
Run Code Online (Sandbox Code Playgroud)

nhu*_*uvy 8

如果捕获错误

在此输入图像描述

解决方案是将“公共”一词更改为“构建”

在此输入图像描述

原来的

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

改成

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

Run Code Online (Sandbox Code Playgroud)

更改index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="logo192.png" />
    <link rel="manifest" href="manifest.json" />
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

结果运行成功无错误

在此输入图像描述


Nik*_*ykh 5

您是否在高级设置下的身份验证选项卡下进行了更改:每个电子邮件地址一个帐户:允许使用同一电子邮件地址创建多个帐户?尝试检查一下,看看是否有问题。