2016-11-15 74 views
0

因此,我正在使用Firebase網絡電子郵件身份驗證,並且遇到了此問題。 Firebase只會檢查密碼是否與電子郵件存在的數據庫中的密碼匹配。如果電子郵件不存在,那麼它不會給出錯誤信息並登錄該用戶。我想知道是否有解決此問題的方法,以便Firebase每次檢查電子郵件時都會檢查,如果不存在,則會返回錯誤消息。Firebase不檢查電子郵件是否爲帳戶

的JavaScript

function SignUserIn() { 
    // Get elements 
    const txtEmail = document.getElementById("txtEmail"); 
    const txtPassword = document.getElementById("txtPassword"); 

    // Get email and pass 
    const email = txtEmail.value; 
    const password = txtPassword.value; 
    const auth = firebase.auth(); 

    //Sign In 
    firebase.auth().signInWithEmailAndPassword(email, password) 
     .catch(function(error) { 
      // Handle Errors here. 
      var errorCode = error.code; 
      var errorMessage = error.message; 
      if (errorCode === 'auth/wrong-password') { 
       window.alert('Wrong password.'); 
      } 
      else { 
       //Realtime listener 
       firebase.auth().onAuthStateChanged(frebaseUser => { 
        if (email) { 
         window.alert("You Are Successfully Signed In! Welcome " + email); 
         window.location = 'homepage.html'; //After successful login, user will be redirected to Homepage 
         sessionStorage.setItem("email", email); 
        } 
        else { 
         console.log('Not logged in'); 
         window.alert("Incorrect User/Password Please Try Again"); 
        } 
       }); 

       sessionStorage.setItem(email); 
      } 

     }); 
} 

HTML BODY

<div class="login-card"> 
       <div class="mlogo"><img src="LogoFINAL copy.png" alt="Medavance"> </div> 

       <h1 class="title">Welcome To Medavance! </h1> 
       <br> 
    <!------------------------------------- Start of Login --------------------------------------------> 
       <input type="email" id="txtEmail" placeholder="Email" required> 
       <input type="password" id="txtPassword" placeholder="Password" required> 
       <button id="btnLogin" class="btn btn-action" onclick="SignUserIn()"> Log in </button> 
       <div class="btn btn-secondary"><a href="NewAccount.html"> Sign Up</div> 
        </div> 
+0

_「火力地​​堡認證使得構建安全的應用程序很容易」 _ - 被安全部分_ **不告訴別人** _他們有錯誤的密碼。你不告訴他們「是的,該用戶存在,但你得到了錯誤的密碼」。你不告訴他們「不,該用戶不存在」。因爲只需找到有效的用戶名就可以攻擊你。你只告訴他們「不能用你的用戶名和密碼登錄你」。 –

+0

@StephenP好的,謝謝,我明白了。但我的主要問題是,如果我輸入的數據庫中不存在的電子郵件,我不會收到錯誤消息,而只是登錄該人,我如何避免這種情況,並至少讓人來檢查用戶存在,如果用戶不存在,則顯示錯誤消息「您無法使用該用戶名和密碼登錄」。 。 – SpiderMonkey

回答

0
firebase.auth().signInWithEmailAndPassword(email, password) 
.then(function() { 
    // Handle success here 
    firebase.auth().onAuthStateChanged(frebaseUser => { 
     sessionStorage.setItem("email", email); 
     window.alert("You Are Successfully Signed In! Welcome " + email); 
     window.location = "homepage.html"; 
    }); 
}, function(error) { 
    // Handle Errors here. 
    var errorMessage = error.message; 
    window.alert(errorMessage); 
    document.getElementById("txtPassword").value = ""; 
}); 

}

0

望着文檔爲signInWithEmailAndPassword,它說你應該,但你」得到auth/user-not-found 「如果有相應的給定電子郵件地址的用戶時拋出」不要在你的測試if (errorCode === 'auth/wrong-password')中檢查那個,你因此執行else,這似乎假定成功。

如果我理解正確,如果有錯誤,signInWithEmailAndPassword只應該調用.catch()回調函數,所以如果調用它,則不應該記錄它們。

您可以檢查具體的errorCode來決定下一步該做什麼,但只記錄下後面的 signInWithEmailAndPassword調用,而不是在catch函數中。這也意味着你需要防止繼續執行。

signInWithEmailAndPassword也返回承諾返回非空firebase.Promise含非空firebase.User」,你可以用下面的調用用戶登錄


免責聲明:這是所有從文檔中,我沒有使用firebase.auth()來測試它。

相關問題