未捕获的TypeError:firebase.auth不是函数

时间:2019-12-16 13:08:06

标签: javascript firebase firebase-authentication

我尝试使用firebase(电子邮件和密码)构建身份验证应用程序,但使用firebase方法存在问题。 当我调用firebase.auth时,他们说“这不是一个函数”。是因为我的项目和Firebase SDK没有链接?

您有什么建议吗? :)

<!doctype html>
<html lang="fr">
  <head>
  <meta charset="utf-8">

  <!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-analytics.js"></script>

<script>
  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "secret",
    authDomain: "secret",
    databaseURL: "secret",
    projectId: "secret",
    storageBucket: "secret",
    messagingSenderId: "secret",
    appId: "secret",
    measurementId: "secret"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>

  <!--Stylesheet CSS --> 
  <link rel="stylesheet" href="../css/main.css">

      <title>Inscription</title>

</head>

<body>

  <form id="formSignUp" method="POST" onsubmit="signUp()">
    <input type="text" id="form_input_mail" name="mail" placeholder="E-mail" size= "30">
    <input type="password" id="form_input_password" name="password" placeholder="Mot de passe" 
      size= "30">
    <button type="submit" id="button_orange_center"> Suivant</button>
  </form>
  <script>
      function signUp(){
      firebase.auth().createUserWithEmailAndPassword(form_input_mail, form_input_password)
    .catch(function(error) {
  // Handle Errors here.
    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);
  });
}
  </script>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

您需要将Firebase身份验证添加到js文件:

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

您可以在此处查看所有要添加的产品:

https://firebase.google.com/docs/web/setup#available-libraries

答案 1 :(得分:1)

Firebase产品(例如Auth)也需要从CDN下载

<script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-auth.js"></script>

参考:https://firebase.google.com/docs/web/setup