Flutter Firebase Anonymous Authentification Sign-In Upon Opening

320

You can do that by await FirebaseAuth.instance.signInAnonymously() it in your main() function before runApp(), so now you will have a user logged when the app starts.

I don't think your second part makes a sense, you will either automatically sign in the user anonymously or give him the options you mentioned which require the click to trigger Google oAuth flow or pass his credentials for the mail login.

Share:
320
Sunshine
Author by

Sunshine

Hi my name is Ihita I like singing and writing My dream is to visit Italy or France

Updated on December 31, 2022

Comments

  • Sunshine
    Sunshine less than a minute

    I currently have an app which Sign's-In a User when a button is tapped by calling :

    Future<void> anonymousSignIn(DocumentSnapshot<Object> document, context) async {
      await FirebaseAuth.instance.signInAnonymously();
      var user = await FirebaseAuth.instance.currentUser;
      var uid = user.uid;
      ScaffoldMessenger.of(context).showSnackBar(getSnackBar("Signed In"));
    }
    

    But I'd like the User to not have to press any button & instead be automatically Signed-In Anonymously when the App is Open - I'm not sure where to write that & how in my App

    Also I ultimately will want to allow the user to also Sign-In with e-mail or Google so before Sign-In Anonymously upon Opening it will have to check if the user was not already Signed-In with those methods

    🙏

  • Sunshine
    Sunshine over 1 year
    thks I upvoted but I'm pretty sure I need to use Sign-In inside a Future thought ?
  • esentis
    esentis over 1 year
    Well yes, you will add the async modifier at your main.
  • esentis
    esentis over 1 year
    You will need to "persist" this information somehow and somewhere, maybe try using the pub.dev/packages/shared_preferences package to store the last sign in information.
  • Sunshine
    Sunshine over 1 year
    ok thks - I wrote it inside Future main() async { } is it correct ?
  • esentis
    esentis over 1 year
    You can remove the Future just async and void main() will do the trick.
  • Sunshine
    Sunshine over 1 year
    i see - I thought Firebase_auth managed all of this & remembered that a user was Signed-In ?
  • esentis
    esentis over 1 year
    Well it does, but if you sign your user anonymously, on the next login the user will still be anonymous. An anonymous user is still a valid user. If you sign in with Google, next time you open the app the user will still be signed on his Google account.
  • Sunshine
    Sunshine over 1 year
    Oh i see so I have to save somehow in Shared Preferences that user had successfully logged into Google the last time & not logged out thank you 🙏
  • Sunshine
    Sunshine over 1 year
    I think I need the Future to call await Firebase.initializeApp(); I'll keep just in case thank you : )