简体   繁体   English

使用电子邮件/密码进行Firebase身份验证

[英]Firebase Authentication using email/password

I'm using below function to login an existing user with firebase authentication, but for some reason nothing happens. 我正在使用以下功能通过Firebase身份验证登录现有用户,但是由于某种原因什么也没有发生。 I don't get an error or confirmation. 我没有收到错误或确认。 I can paste the same code into my DOM console and get logged in, but for some reason nothing when I call this function from my JS file. 我可以将相同的代码粘贴到DOM控制台中并登录,但是由于某种原因,当我从JS文件中调用此函数时,什么也没做。 I've confirmed that the input's for email/password are right, and believe this may be an Async issue, but not sure how to go upon resolving it. 我已经确认电子邮件/密码的输入正确,并且相信这可能是异步问题,但不确定如何解决。

 $('.user-login').on("submit", function () {
   var email = $('.email-login').val().trim();
   var password = $('.password-login').val().trim();
   console.log(email + ' ' + password)
firebase.auth().signInWithEmailAndPassword(email, password)
  .then(function () {
    console.log('logged in')
  })
  .catch(function (error) {
    console.log(error);
  });

}) })

.onAuthStateChanged is how to handle the signin. .onAuthStateChanged是如何处理登录的方法。

 firebase.auth().onAuthStateChanged(function(user) { if (user) { // User is signed in. } }); 

I am using this code from a while and its working perfectly fine on all browsers. 我曾经使用过这段代码,并且在所有浏览器上都能正常工作。 and this will also provide you with errors in case they occur. 如果发生错误,这还将为您提供错误。

function signIn() {
  var email = document.querySelector("#email").value;
  var password = document.querySelector("#password").value;
  firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    window.alert(errorMessage);
  });
}

and this to handle events on user state change like he login and logout. 这样就可以处理用户状态更改(例如他的登录和注销)事件。

var user = firebase.auth().currentUser;

    firebase.auth().onAuthStateChanged(function (user) {
          if (user) {

        }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 电子版Firebase电子邮件/密码身份验证 - Firebase email/password authentication in electron Polymer firebase。通过密码和电子邮件进行身份验证 - Polymer firebase.. Authentication with password and email firebase SERVER_ERROR的电子邮件/密码身份验证 - email/password authentication for firebase SERVER_ERROR 尝试让 Firebase 电子邮件/密码身份验证与 React Native 一起使用 - Trying to get the Firebase Email/Password Authentication to work with React Native 登录使用电子邮件和密码创建的 firebase 帐户时密码消失 - Password is gone when logging in a firebase account that was created using Email and Password Firebase Email 链路认证 - Firebase Email Link Authentication 使用 firebase admin sdk 创建用户,可以使用 email 和密码登录 - Create user with firebase admin sdk that can signIn using email and password Cordova / Phonegap中的Firebase:在应用程序内使用电子邮件/密码登录? - Firebase in Cordova/Phonegap: Log in using Email/Password from within app? Firebase 数据库用户身份验证:使用来自电话提供商的 UID 创建用户 Email 和密码 - Firebase Database User Authentication: Create User Email and Password with UID from Phone Provider Firebase身份验证(电子邮件和密码)未进行身份验证 - Firebase auth (email and password) not authenticating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM