I am giving Users the option to sign in via Email and Password or anonymously.
Creating and signing in the User with Email and Password works fine, but I am having problems with displaying different contents for anonymously signed in users.
This is how I create them:
final _auth = FirebaseAuth.instance;
// Create Anonymous User
Future signInAnonymously() {
// return _auth.signInAnonymously();
_auth.signInAnonymously();
}
If a user chooses to get into the app anonymously in the Auth Form, I trigger following function:
Future submitAnon() async {
await signInAnonymously();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
);
}
I am using a Future Builder / Stream to listen to Auth Changes in my main.dart:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
return FutureBuilder(
// Initialize FlutterFire:
future: _initialization,
builder: (context, appSnapshot) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'myapp',
home: appSnapshot.connectionState != ConnectionState.done
? SplashScreen()
: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (ctx, userSnapshot) {
if (userSnapshot.connectionState ==
ConnectionState.waiting) {
return SplashScreen();
}
if (userSnapshot.hasData) {
return HomeScreen();
}
return AuthScreen();
}),
);
});
}
}
What I tried was passing a "isAnon=true" boolean to HomeScreen in submitAnon() but once the user re-opens the app, he is getting into the app from main.dart, where all the users that signed-up via email also get in. Is there a good way to check inside the App if the User is authenticated anonymously to build my widgets depending on that? e.g. showing Authentication Options instead of actual content that is only directed to e-mail&passsword users?
I think it's too late but here's the simplest way:
FirebaseAuth.instance.currentUser!.isAnonymous // returns a bool
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