Anonymous user in Firebase

不打扰是莪最后的温柔 提交于 2019-12-24 06:15:10

问题


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?


回答1:


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;
      // ...
    });
  }
});


来源:https://stackoverflow.com/questions/44195616/anonymous-user-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!