Flutter Firebase Anonymous Authentification Sign-In Upon Opening
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.

Sunshine
Hi my name is Ihita I like singing and writing My dream is to visit Italy or France
Updated on December 31, 2022Comments
-
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 over 1 yearthks I upvoted but I'm pretty sure I need to use Sign-In inside a Future thought ?
-
esentis over 1 yearWell yes, you will add the
async
modifier at yourmain
. -
esentis over 1 yearYou 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 over 1 yearok thks - I wrote it inside Future main() async { } is it correct ?
-
esentis over 1 yearYou can remove the
Future
justasync
andvoid main()
will do the trick. -
Sunshine over 1 yeari see - I thought Firebase_auth managed all of this & remembered that a user was Signed-In ?
-
esentis over 1 yearWell 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 over 1 yearOh 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 over 1 yearI think I need the Future to call
await Firebase.initializeApp();
I'll keep just in case thank you : )