Uncaught TypeError: firebase.auth is not a function

Deadly 提交于 2020-12-23 13:16:31

问题


I try to build an authentification app with firebase (email & password) but i have problem with firebase methods. When I call firebase.auth, they said "it's not a function". Is it because my project and firebase SDK are not linked ?

Do you have some suggestions ? :)

<!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>


回答1:


Firebase products (eg. Auth) also need to be downloaded from the CDN

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

Reference: https://firebase.google.com/docs/web/setup




回答2:


You need to add firebase auth to the js file:

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

You can check here for all available products to add:

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



来源:https://stackoverflow.com/questions/59357268/uncaught-typeerror-firebase-auth-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!