使用其身份验证方法在Firebase上创建用户

时间:2018-07-04 22:13:39

标签: javascript web cors firebase-authentication

我尝试发送凭据,并且出现此错误。

  

跨域请求被阻止:相同来源策略禁止读取https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key= *******处的远程资源。 (原因:CORS请求未成功。)

(function () {

  var config = {
    apiKey: "*******",
    authDomain: "*******",
    databaseURL: "*******",
    projectId: "*******",
    storageBucket: "*******",
    messagingSenderId: "*******"
  };
  firebase.initializeApp(config);

  const email = document.getElementById('email');
  const password = document.getElementById('password');
  const btnSignUp = document.getElementById('btnSignUp');

  btnSignUp.addEventListener('click', e => {
    const mail = email.value;
    const pass = password.value;

    firebase.auth().createUserWithEmailAndPassword(mail, pass).catch(function(error) {

      var errorCode = error.code;
      var errorMessage = error.message;

      if (errorCode == 'auth/weak-password') {
        alert('The password is too weak.');
      } else {
        alert(errorMessage);
      }
      console.log(error);

    });
  });
}());

我该如何解决CORS请求被阻止的问题?

1 个答案:

答案 0 :(得分:0)

您的Firebase导入错误,请使用此:

<!-- Firebase App is always required and must be first -->
<script src="https://www.gstatic.com/firebasejs/5.1.0/firebase-app.js"></script>

<!-- Add additional services that you want to use -->
<script src="https://www.gstatic.com/firebasejs/5.1.0/firebase-auth.js"></script>