Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the same firebase anonymous user in a flutter app

I'm currently developing a Flutter app for in a internal project whose process is :

  • You can log in as an authenticated user (mail / passwd) and complete some actions / tasks / validate things
  • You can log anonymously and complete some actions, BUT you need to validate these actions through an authenticated user (AU).

By doing so, each time an AU log himself, he can validate and then switch back to anonymous mode. The problem is that Firebase creates a new anonymous user each time.

The app could be utilized on multiple devices across the company, which could create 100's of anonymous users a day, while in fact it was only 6-7 users, so what can I do to avoid this ?

I've read about customIdToken but I haven't come to a solution for my problem.

Here is the code I'm using:

Future<FirebaseUser> signInAnonToken(String token) async {
    FirebaseUser user = await _firebaseAuth.signInWithCustomToken(token: token);
    return user;
}

FirebaseUser userAnon = await widget.auth.signInAnonToken("useranonuid");

Where "useranonuid" is the uid of the anonymous user but also the token I get by using the getIdToken(refresh:true) method

Thanks in advance.

like image 643
extazx2 Avatar asked Feb 25 '19 09:02

extazx2


1 Answers

The problem is that Firebase creates a new anonymous user each time.

The workaround that I did on my end is to add deleting the anonymous user on the end of each workflow. This should prevent the Firebase project hitting the 100 million anonymous user account limit.

There's no way to restore previous Firebase Anonymous Auth instance. That would defeat the purpose of being "anonymous" in the first place.

like image 67
Omatt Avatar answered Sep 28 '22 06:09

Omatt