Remove loggedIn global variable

This commit is contained in:
Reimar 2024-08-27 08:40:54 +02:00
parent de8d898289
commit 384f25ddbe
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
5 changed files with 45 additions and 48 deletions

View File

@ -16,6 +16,7 @@ class SideMenu extends StatefulWidget {
class _SideMenuState extends State<SideMenu> {
late int _selectedIndex;
bool _isLoggedIn = false;
@override
void initState() {
@ -30,7 +31,6 @@ class _SideMenuState extends State<SideMenu> {
prefs.remove('id');
setState(() {
loggedIn = false;
user = null;
});
@ -46,7 +46,7 @@ class _SideMenuState extends State<SideMenu> {
api.isLoggedIn(context).then((value) {
setState(() {
loggedIn = value; // Update the second variable here
_isLoggedIn = value;
});
});
}
@ -110,7 +110,7 @@ class _SideMenuState extends State<SideMenu> {
thickness: 2,
indent: 40,
),
...(loggedIn
...(_isLoggedIn
? [
ListTile(
title: const Text('Log out'),

View File

@ -1,6 +1,4 @@
import 'package:mobile/models.dart';
//Global variables
bool loggedIn = false;
User? user;

View File

@ -47,37 +47,41 @@ class _ProfilePageState extends State<EditProfilePage> {
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<EditProfilePage> {
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<EditProfilePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
return Scaffold(
appBar: AppBar(
title: Text('Edit Profile'),
leading: IconButton(

View File

@ -34,10 +34,6 @@ class _LoginPageState extends State<LoginPage> {
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')));
Navigator.pushReplacementNamed(context, '/home');

View File

@ -25,13 +25,12 @@ class _ProfilePageState extends State<ProfilePage> {
}
Future<void> 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;
}