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
🙏
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.
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAuth.instance.signInAnonymously();
var user = await FirebaseAuth.instance.currentUser;
var uid = user.uid;
runApp(child : App());
}
This is the full solution I ended up writing thanks to @esentis
but I'm still wondering - how to check if user had not logged In with Google the last time it open the App
to log it in with Google Automatically without pressing button instead of Anonymously
esentis suggested using Shared Preferences to store the last login State - I will try to post my code when I have implemented it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With