From 384f25ddbece23fd94362094bd0ef8166579cc16 Mon Sep 17 00:00:00 2001 From: Reimar Date: Tue, 27 Aug 2024 08:40:54 +0200 Subject: [PATCH] Remove loggedIn global variable --- Mobile/lib/base/sidemenu.dart | 6 +-- Mobile/lib/base/variables.dart | 4 +- Mobile/lib/editprofile.dart | 72 ++++++++++++++++++---------------- Mobile/lib/login.dart | 4 -- Mobile/lib/profile.dart | 7 ++-- 5 files changed, 45 insertions(+), 48 deletions(-) diff --git a/Mobile/lib/base/sidemenu.dart b/Mobile/lib/base/sidemenu.dart index ed1a718..089b470 100644 --- a/Mobile/lib/base/sidemenu.dart +++ b/Mobile/lib/base/sidemenu.dart @@ -16,6 +16,7 @@ class SideMenu extends StatefulWidget { class _SideMenuState extends State { late int _selectedIndex; + bool _isLoggedIn = false; @override void initState() { @@ -30,7 +31,6 @@ class _SideMenuState extends State { prefs.remove('id'); setState(() { - loggedIn = false; user = null; }); @@ -46,7 +46,7 @@ class _SideMenuState extends State { api.isLoggedIn(context).then((value) { setState(() { - loggedIn = value; // Update the second variable here + _isLoggedIn = value; }); }); } @@ -110,7 +110,7 @@ class _SideMenuState extends State { thickness: 2, indent: 40, ), - ...(loggedIn + ...(_isLoggedIn ? [ ListTile( title: const Text('Log out'), diff --git a/Mobile/lib/base/variables.dart b/Mobile/lib/base/variables.dart index 9eb7224..3816d9c 100644 --- a/Mobile/lib/base/variables.dart +++ b/Mobile/lib/base/variables.dart @@ -1,6 +1,4 @@ import 'package:mobile/models.dart'; //Global variables -bool loggedIn = false; -User? user; - +User? user; diff --git a/Mobile/lib/editprofile.dart b/Mobile/lib/editprofile.dart index bb438c6..ec66acf 100644 --- a/Mobile/lib/editprofile.dart +++ b/Mobile/lib/editprofile.dart @@ -47,37 +47,41 @@ class _ProfilePageState extends State { const SnackBar(content: Text('Passwords do not match'))); return; } - final prefs = await SharedPreferences.getInstance(); - String? id = prefs.getString('id'); - final response = await api - .request(context, api.ApiService.auth, 'PUT', '/api/users', { + final prefs = await SharedPreferences.getInstance(); + String? id = prefs.getString('id'); + + if (!mounted) { + return; + } + + final response = await api.request(context, api.ApiService.auth, 'PUT', '/api/users', { 'id' : id, 'username': usernameInput.text, 'email': emailInput.text, 'password': passwordInput.text, }); - useMAN.log('data'); + if (!mounted) { + return; + } + useMAN.log('data'); - if (response!.isEmpty) { - prefs.remove('token'); - loggedIn = true; - user = User( - id!, - emailInput.text, - usernameInput.text, - DateTime.now(), - ); - Navigator.of(context).pop(); // Close the dialog + if (response != null) { + user = User( + id!, + emailInput.text, + usernameInput.text, + DateTime.now(), + ); - } - else{ - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Something went wrong! Please contact an admin.')), - ); - } + Navigator.of(context).pop(); // Close the dialog + } else { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Something went wrong! Please contact an admin.')), + ); + } } void _deleteProfile(BuildContext context) { @@ -101,21 +105,21 @@ class _ProfilePageState extends State { final prefs = await SharedPreferences.getInstance(); String? id = prefs.getString('id'); - final response = await api - .request(context, api.ApiService.auth, 'DELETE', '/api/users/$id', null); + final response = await api.request(context, api.ApiService.auth, 'DELETE', '/api/users/$id', null); - if (response!.isEmpty) { + if (response != null) { prefs.remove('token'); - prefs.remove('id'); - setState(() { - loggedIn = false; - user = null; + prefs.remove('id'); + + setState(() { + user = null; }); - Navigator.of(context).pop(); // Close the dialog - Navigator.of(context).pop(); - Navigator.pushReplacementNamed(context, '/register'); - } - else{ + + Navigator.of(context).pop(); // Close the dialog + Navigator.of(context).pop(); + + Navigator.pushReplacementNamed(context, '/register'); + } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Something went wrong! Please contact an admin.')), ); @@ -130,7 +134,7 @@ class _ProfilePageState extends State { @override Widget build(BuildContext context) { - return Scaffold( + return Scaffold( appBar: AppBar( title: Text('Edit Profile'), leading: IconButton( diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index 31fa313..dcf17d4 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -33,10 +33,6 @@ class _LoginPageState extends State { final prefs = await SharedPreferences.getInstance(); prefs.setString('token', jsonUser.token); prefs.setString('id', jsonUser.id); - - setState(() { - loggedIn == true; - }); if (mounted) { ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged in'))); diff --git a/Mobile/lib/profile.dart b/Mobile/lib/profile.dart index 129506e..3f97794 100644 --- a/Mobile/lib/profile.dart +++ b/Mobile/lib/profile.dart @@ -25,13 +25,12 @@ class _ProfilePageState extends State { } Future getProfile() async { - if (!loggedIn) { + if (!await api.isLoggedIn(context)) { WidgetsBinding.instance.addPostFrameCallback((_) { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Please log in')), - ); + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Please log in to view this page'))); Navigator.pushReplacementNamed(context, '/login'); }); + return; }