簡體   English   中英

Firebase身份驗證(電子郵件和密碼)未進行身份驗證

[英]Firebase auth (email and password) not authenticating

我無法讓firebase auth工作。 在DB中啟用了用戶身份驗證,並且已創建登錄ID。 何時使用瀏覽器的調試控制台,只有1個警告:

It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import the individual SDK components you intend to use.

我不確定我在這里缺少什么。 任何幫助是極大的贊賞。

 function login(){ const userEmail = document.getElementById('userEmail'); const userPassword = document.getElementById('userPassword'); const loginBtn = document.getElementById('loginBtn'); loginBtn.addEvenListener('click'), e => { const email = userEmail.value; const pass = userPassword.value; } firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { var errorCode = error.code; var errorMessage = error.message; if (errorCode === 'auth/wrong-password') { alert('Wrong password.'); } else { alert(errorMessage); } console.log(error); }); firebase.auth().onAuthStateChanged(user => { if (user) { window.location = 'secondpage.html'; } }); } 
 <!DOCTYPE html> <html> <head> <title>TEST</title> <h1>TESTING</h1> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="author" content="Name" <link rel="stylesheet" href="style.css" /> </head> <body> <div id="login_div" class="main-div"> <h3>Welcome</h3> <input id="userEmail" type="email" placeholder="Email..." /> <input id="userPassword" type="password" placeholder="Password..." /> <button click="login()">Login to Account</button> </div> <script src="https://www.gstatic.com/firebasejs/5.11.0/firebase.js"></script> <script> // Initialize Firebase var config = { apiKey: "KEY", authDomain: "name.firebaseapp.com", databaseURL: "https://name.firebaseio.com", projectId: "testlist", storageBucket: "testlist.appspot.com", messagingSenderId: "111111111111" }; firebase.initializeApp(config); </script> <script src="test.js"></script> </body> </html> 

試試這個代碼!

你的按鈕的問題。

<button click="login()">Login to Account</button>

click不是有效事件, onclick有效。 所以你的按鈕看起來像這樣:

<button onclick="login()">Login to Account</button>
<html>
<head>
<title>TEST</title>
<h1>TESTING</h1>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Name"
<link rel="stylesheet" href="style.css" />
</head>

<body>
<div id="login_div" class="main-div">
<h3>Welcome</h3>
<input id="userEmail" type="email" placeholder="Email..." />
<input id="userPassword" type="password" placeholder="Password..." />

<button onclick="login()">Login to Account</button>
</div>

<script src="https://www.gstatic.com/firebasejs/5.11.0/firebase.js"></script>
   <script>
//   Initialize Firebase
    var config = {
    apiKey: "KEY",
    authDomain: "name.firebaseapp.com",
    databaseURL: "https://name.firebaseio.com",
    projectId: "testlist",
    storageBucket: "testlist.appspot.com",
    messagingSenderId: "111111111111"
    };
    firebase.initializeApp(config);
  firebase.initializeApp(config);
    </script>

    <script src="test.js"></script>
    </body>
  </html>

<_TEST.js-> 


function login() {
  const userEmail = document.getElementById("userEmail");
  const userPassword = document.getElementById("userPassword");

  const email = userEmail.value;
  console.log("TCL: login -> email", email);
  const pass = userPassword.value;
  console.log("TCL: login -> pass", pass);

  firebase
    .auth()
    .signInWithEmailAndPassword(email, pass)
    .catch(function(error) {
      var errorCode = error.code;
      var errorMessage = error.message;
      if (errorCode === "auth/wrong-password") {
        alert("Wrong password.");
      } else {
        alert(errorMessage);
      }
      console.log(error);
    });
  firebase.auth().onAuthStateChanged(user => {
    console.log("TCL: login -> user", user);
    if (user) {
       window.location = "secondpage.html";
    }
  });
}

您使用的是錯誤的腳本。 您正在使用:

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

但你需要使用:

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

在許多論壇中已經指出/ firebase之一給出了這個錯誤,因為你需要/ firebase-app。 剛剛獲得firebase可能導入的方式比您實際需要的多,並且它會使您的應用程序大小膨脹

您的身份驗證也可能需要:

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


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM