[英]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.