Firebase中的匿名用户 [英] Anonymous user in Firebase
问题描述
由于AMT身份验证与Firebase无关,因此我无法使用这些身份验证用户身份。为了能够将数据写入Firebase,必须存在身份验证。
我目前的解决方案是使用匿名用户。虽然它工作,我发现每次页面重新加载,一个新的匿名用户被创建。这可能不是可扩展的,因为在开发过程中,我创建了约6000个匿名用户。
我怎样才能解决这个问题?
听起来像是调用 signInAnonymously()
,这确实会创建一个新的匿名用户。
您应该改为监视身份验证状态,并且只有在用户尚未登录时才登录:
firebase.auth()。onAuthStateChanged(用户){
if(user){
//用户登录。
} else {
firebase.auth()。 ()错误
var errorCode = error.code;
var errorMessage = error.message;
//。 ..
});
}
});
I'm running a crowdsourcing website with Amazon Mechanical Turk. Firebase is used to track intermediate information of users.
Since AMT authentication is not related to Firebase, I can't use those to authenticate user. And to be able to write data to Firebase, the authentication must be present.
My current solution is using anonymous users. While it works, I found that every time the page is reloaded, a new anonymous user is created. This might not be scalable, as during development I have created ~6000 anonymous users.
How could I fix this problem?
It sounds like you're calling signInAnonymously()
on every page load, which will indeed create a new anonymous user.
You should instead monitor authentication state and only sign in the user when they're not signed in yet:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
firebase.auth().signInAnonymously().catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
}
});
这篇关于Firebase中的匿名用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!