diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 2f11dcd..827a55d 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -3,6 +3,9 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; +import 'base/variables.dart'; + + enum ApiService { auth, app, @@ -61,7 +64,10 @@ Future isLoggedIn(BuildContext context) async { final prefs = await SharedPreferences.getInstance(); final token = prefs.getString('token'); - if (token == null) return false; + if (token == null){ + loggedIn = false; + return false; + } try { String base64 = token.split('.')[1]; diff --git a/Mobile/lib/base/sidemenu.dart b/Mobile/lib/base/sidemenu.dart index 6b410d0..1798fbc 100644 --- a/Mobile/lib/base/sidemenu.dart +++ b/Mobile/lib/base/sidemenu.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:mobile/api.dart' as api; import 'package:shared_preferences/shared_preferences.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'variables.dart'; class SideMenu extends StatefulWidget { @@ -15,7 +16,6 @@ class SideMenu extends StatefulWidget { } class _SideMenuState extends State { - bool _isLoggedIn = false; late int _selectedIndex; @override @@ -28,7 +28,10 @@ class _SideMenuState extends State { final prefs = await SharedPreferences.getInstance(); prefs.remove('token'); - setState(() => _isLoggedIn = false); + setState(() { + loggedIn = false; + }); + if (mounted) { ScaffoldMessenger.of(context).showSnackBar( @@ -43,8 +46,12 @@ class _SideMenuState extends State { super.didChangeDependencies(); api - .isLoggedIn(context) - .then((value) => setState(() => _isLoggedIn = value)); + .isLoggedIn(context).then((value) { + setState(() { + loggedIn = value; // Update the second variable here + }); +}); + } @override @@ -114,7 +121,7 @@ Widget build(BuildContext context) { thickness: 2, indent: 40, ), - ...(_isLoggedIn + ...(loggedIn ? [ ListTile( title: const Text('Log out'), diff --git a/Mobile/lib/base/variables.dart b/Mobile/lib/base/variables.dart new file mode 100644 index 0000000..9eb7224 --- /dev/null +++ b/Mobile/lib/base/variables.dart @@ -0,0 +1,6 @@ +import 'package:mobile/models.dart'; + +//Global variables +bool loggedIn = false; +User? user; + diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index f5d2432..5b59fe1 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -1,7 +1,10 @@ import 'package:flutter/material.dart'; import 'package:mobile/base/sidemenu.dart'; +import 'package:mobile/base/variables.dart'; +import 'package:mobile/models.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'dart:convert'; import 'api.dart' as api; class LoginPage extends StatefulWidget { @@ -16,16 +19,28 @@ class _LoginPageState extends State { final passwordInput = TextEditingController(); Future _login() async { - final token = await api - .request(context, api.ApiService.auth, 'POST', '/api/Users/login', { - 'email': emailInput.text, - 'password': passwordInput.text, - }); + final token = await api + .request(context, api.ApiService.auth, 'POST', '/api/Users/login', { + 'email': emailInput.text, + 'password': passwordInput.text, + }); - if (token == null) return; + if (token == null) return; + + // Assuming token is a JSON string + Map json = jsonDecode(token); + User jsonUser = User.fromJson(json); final prefs = await SharedPreferences.getInstance(); - prefs.setString('token', token); + prefs.setString('token', jsonUser.token); + prefs.setString('id', jsonUser.id); + + + setState(() + {user = jsonUser; + loggedIn == true; + }); + if (mounted) { ScaffoldMessenger.of(context).showSnackBar( diff --git a/Mobile/lib/models.dart b/Mobile/lib/models.dart index 5ab25e3..f435a3b 100644 --- a/Mobile/lib/models.dart +++ b/Mobile/lib/models.dart @@ -7,9 +7,23 @@ class Favorite { Favorite(this.id, this.userId, this.lat, this.lng); } -class Login { - String id; +class User { String token; + String id; + String email; + String username; + DateTime createdAt; + + User(this.token, this.id, this.email, this.username, this.createdAt); + + factory User.fromJson(Map json) { + return User( + json['token'], + json['id'], + json['email'], + json['username'], + DateTime.parse(json['createdAt']), + ); + } +} - Login(this.id, this.token); -} \ No newline at end of file diff --git a/Mobile/lib/profile.dart b/Mobile/lib/profile.dart index 51d7622..3b83790 100644 --- a/Mobile/lib/profile.dart +++ b/Mobile/lib/profile.dart @@ -1,44 +1,46 @@ import 'package:flutter/material.dart'; +import 'package:mobile/base/variables.dart'; +import 'package:mobile/models.dart'; import 'base/sidemenu.dart'; -import 'package:shared_preferences/shared_preferences.dart'; -import 'api.dart' as api; +import 'package:mobile/base/variables.dart'; + + class ProfilePage extends StatefulWidget { - final String id; - const ProfilePage({super.key, required this.id}); + const ProfilePage({super.key}); + //const ProfilePage({super.key, required this.id}); + @override State createState() => _ProfilePageState(); } class _ProfilePageState extends State { - late String _id; + late User userData; - @override - void initState() { + @override + void initState() { super.initState(); - _id = widget.id; // Initialize _selectedIndex with the value from the widget - } -Future _getUser() async { - final token = await api - .request(context, api.ApiService.auth, 'GET', '/api/Users/$_id', { - }); - - if (token == null) return; - - final prefs = await SharedPreferences.getInstance(); - prefs.setString('token', token); - - if (mounted) { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Successfully logged in'))); - Navigator.pushReplacementNamed(context, '/home'); + // Check if the user is logged in when the page is entered + if (loggedIn == false) { + WidgetsBinding.instance.addPostFrameCallback((_) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Please log in'))); + Navigator.pushReplacementNamed(context, '/login'); + }); } + + setState(() { + userData = user!; + }); } + + + @override Widget build(BuildContext context) { return SideMenu(