Dzung Nguyen
Dzung Nguyen

Reputation: 3942

Anonymous user in Firebase

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?

Upvotes: 2

Views: 725

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

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

Upvotes: 7

Related Questions