gui login state stays right after closing and reopening the app

This commit is contained in:
2023-05-30 17:01:40 +02:00
parent fe4f1d52ae
commit 4c548b030c
2 changed files with 21 additions and 11 deletions
+1 -1
View File
@@ -338,7 +338,7 @@ class _GenericListState extends State<GenericList> {
), ),
floatingActionButton: Consumer<AuthState>( floatingActionButton: Consumer<AuthState>(
builder: (context, authState, child) { builder: (context, authState, child) {
if (authState.isSignedIn) { if (authState.user != null) {
return FloatingActionButton( return FloatingActionButton(
onPressed: () { onPressed: () {
clearSelectedItems(); clearSelectedItems();
+20 -10
View File
@@ -4,15 +4,26 @@ import 'package:provider/provider.dart';
class AuthState extends ChangeNotifier { class AuthState extends ChangeNotifier {
FirebaseAuth auth = FirebaseAuth.instance; FirebaseAuth auth = FirebaseAuth.instance;
bool _isSignedIn = false; User? _user;
bool get isSignedIn => _isSignedIn; User? get user => _user;
AuthState() {
auth.authStateChanges().listen((User? user) {
if (user == null) {
print('User is currently signed out!');
_user = null;
} else {
print('User is signed in!');
_user = user;
}
notifyListeners();
});
}
Future<void> signInAnonymously() async { Future<void> signInAnonymously() async {
try { try {
await auth.signInAnonymously(); await auth.signInAnonymously();
_isSignedIn = true;
notifyListeners();
} catch (e) { } catch (e) {
print(e); print(e);
} }
@@ -20,10 +31,9 @@ class AuthState extends ChangeNotifier {
Future<void> signOut() async { Future<void> signOut() async {
await auth.signOut(); await auth.signOut();
_isSignedIn = false;
notifyListeners();
} }
} }
class AnonymAuthScreen extends StatelessWidget { class AnonymAuthScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -32,10 +42,10 @@ class AnonymAuthScreen extends StatelessWidget {
child: Consumer<AuthState>( child: Consumer<AuthState>(
builder: (context, authState, child) { builder: (context, authState, child) {
return ElevatedButton( return ElevatedButton(
onPressed: authState.isSignedIn onPressed: authState.user == null
? authState.signOut ? authState.signInAnonymously
: authState.signInAnonymously, : authState.signOut,
child: Text(authState.isSignedIn ? 'Abmelden' : 'Anonym anmelden'), child: Text(authState.user == null ? 'Anonym anmelden' : 'Abmelden'),
); );
}, },
), ),