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

View File

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

View File

@ -47,37 +47,41 @@ class _ProfilePageState extends State<EditProfilePage> {
const SnackBar(content: Text('Passwords do not match'))); const SnackBar(content: Text('Passwords do not match')));
return; return;
} }
final prefs = await SharedPreferences.getInstance();
String? id = prefs.getString('id');
final response = await api final prefs = await SharedPreferences.getInstance();
.request(context, api.ApiService.auth, 'PUT', '/api/users', { String? id = prefs.getString('id');
if (!mounted) {
return;
}
final response = await api.request(context, api.ApiService.auth, 'PUT', '/api/users', {
'id' : id, 'id' : id,
'username': usernameInput.text, 'username': usernameInput.text,
'email': emailInput.text, 'email': emailInput.text,
'password': passwordInput.text, 'password': passwordInput.text,
}); });
useMAN.log('data'); if (!mounted) {
return;
}
useMAN.log('data');
if (response!.isEmpty) { if (response != null) {
prefs.remove('token'); user = User(
loggedIn = true; id!,
user = User( emailInput.text,
id!, usernameInput.text,
emailInput.text, DateTime.now(),
usernameInput.text, );
DateTime.now(),
);
Navigator.of(context).pop(); // Close the dialog
} Navigator.of(context).pop(); // Close the dialog
else{ } else {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Something went wrong! Please contact an admin.')), const SnackBar(content: Text('Something went wrong! Please contact an admin.')),
); );
} }
} }
void _deleteProfile(BuildContext context) { void _deleteProfile(BuildContext context) {
@ -101,21 +105,21 @@ class _ProfilePageState extends State<EditProfilePage> {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
String? id = prefs.getString('id'); String? id = prefs.getString('id');
final response = await api final response = await api.request(context, api.ApiService.auth, 'DELETE', '/api/users/$id', null);
.request(context, api.ApiService.auth, 'DELETE', '/api/users/$id', null);
if (response!.isEmpty) { if (response != null) {
prefs.remove('token'); prefs.remove('token');
prefs.remove('id'); prefs.remove('id');
setState(() {
loggedIn = false; setState(() {
user = null; user = null;
}); });
Navigator.of(context).pop(); // Close the dialog
Navigator.of(context).pop(); Navigator.of(context).pop(); // Close the dialog
Navigator.pushReplacementNamed(context, '/register'); Navigator.of(context).pop();
}
else{ Navigator.pushReplacementNamed(context, '/register');
} else {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Something went wrong! Please contact an admin.')), const SnackBar(content: Text('Something went wrong! Please contact an admin.')),
); );
@ -130,7 +134,7 @@ class _ProfilePageState extends State<EditProfilePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text('Edit Profile'), title: Text('Edit Profile'),
leading: IconButton( leading: IconButton(

View File

@ -33,10 +33,6 @@ class _LoginPageState extends State<LoginPage> {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
prefs.setString('token', jsonUser.token); prefs.setString('token', jsonUser.token);
prefs.setString('id', jsonUser.id); prefs.setString('id', jsonUser.id);
setState(() {
loggedIn == true;
});
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged in'))); ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged in')));

View File

@ -25,13 +25,12 @@ class _ProfilePageState extends State<ProfilePage> {
} }
Future<void> getProfile() async { Future<void> getProfile() async {
if (!loggedIn) { if (!await api.isLoggedIn(context)) {
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Please log in to view this page')));
const SnackBar(content: Text('Please log in')),
);
Navigator.pushReplacementNamed(context, '/login'); Navigator.pushReplacementNamed(context, '/login');
}); });
return; return;
} }