[英]Google firebase web: User authentication and database write
I have just started with firebase for my webapp and here is my code and problem beneath. 我刚刚为我的Webapp开始使用Firebase,这是下面的代码和问题。
<script src="https://www.gstatic.com/firebasejs/4.1.2/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.1.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.1.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.1.1/firebase-database.js"></script>
<script>
// Initialize Firebase
var config = {
...
};
firebase.initializeApp(config);
</script>
<script>
function handleSignUp() {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
firebase.auth().createUserWithEmailAndPassword(email, 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);
});
}
function initApp() {
document.getElementById('quickstart-sign-up').addEventListener('click', handleSignUp, false);
}
window.onload = function() {
initApp();
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log('yes');
I want data persistence in firebase database at this step.
我想在此步骤中在Firebase数据库中保持数据持久性。
} else {
console.log('no');
}
});
};
</script>
Problem is whenever I am refreshing my page, as the user is authenticated, console returns Yes (when I am adding the database push logic, data is being saved again and again whenever I am refreshing the page. I have tried moving the console.log OR database push to .onAuthStateChange
to my initApp()
and handleSignUp()
functions as well. But to no avail. 问题是,每当我刷新页面时,通过用户身份验证,控制台就会返回“是”(当我添加数据库推送逻辑时,每当刷新页面时,一次又一次地保存数据。我曾尝试移动console.log或将数据库推送到
.onAuthStateChange
以及我的initApp()
和handleSignUp()
函数,但无济于事。
As per firebase docs , I tried the below as well: 根据firebase文档 ,我也尝试了以下方法:
var user = firebase.auth().currentUser;
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
However, this almost always returns a console.log
as No. I am thinking that since it takes some time for the authentication to happen, the user check is skipping authentication and appearing as if No user is authenticated. 但是,这几乎总是将
console.log
返回为No。我在想,由于进行身份验证需要花费一些时间,因此用户检查正在跳过身份验证,并且看起来好像没有用户通过身份验证。 The ask is I want the user to be signed up (and automatically logged in which happens anyway with firebase) and user details (along with additional details which I am asking the user to fill in at Sign Up) to be saved in firebase database one time only. 我想问的是我希望用户注册(并自动登录,无论如何使用firebase都会发生)和用户详细信息(以及我要求用户在注册时填写的其他详细信息)保存在firebase数据库中只有时间。 Any pointer would be appreciated here.
任何指针在这里将不胜感激。
Think I got it. 认为我明白了。 The use of then (promises):
然后(承诺)的使用:
firebase.auth().createUserWithEmailAndPassword(email, password).then(function() {
var user = firebase.auth().currentUser;
console.log(user.email);
}).catch(function(error)
As soon as user is created, the function will act (will do the data persistence in the database) and since it is not dependent on user auth'ed or not, along with unique emails, this step would never be repeated, hence single time data save in database. 创建用户后,该功能将立即起作用(将在数据库中进行数据持久化),并且由于它不依赖于用户是否经过身份验证以及唯一的电子邮件,因此永远不会重复此步骤,因此只需一次数据保存在数据库中。 Had gone through the docs multiple times, somehow missed it.
已经多次浏览了文档,以某种方式错过了它。 Apologies guys.
道歉的家伙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.