diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 25b6bdc..5bf781f 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -8,8 +8,8 @@ enum ApiService { app, } -Future request(BuildContext context, ApiService service, String method, String path, Object? body) async { - final messenger = ScaffoldMessenger.of(context); +Future request(BuildContext? context, ApiService service, String method, String path, Object? body) async { + final messenger = context != null ? ScaffoldMessenger.of(context) : null; final prefs = await SharedPreferences.getInstance(); final host = switch (service) { @@ -42,17 +42,20 @@ Future request(BuildContext context, ApiService service, String method, body: body != null ? jsonEncode(body) : null, ); } - } catch (_) { - messenger.showSnackBar(const SnackBar(content: Text('Unable to connect to server'))); + } catch (e) { + debugPrint(e.toString()); + messenger?.showSnackBar(const SnackBar(content: Text('Unable to connect to server'))); return null; } if (response.statusCode < 200 || response.statusCode >= 300) { try { final json = jsonDecode(response.body); - messenger.showSnackBar(SnackBar(content: Text(json['message']))); - } catch (_) { - messenger.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})'))); + messenger?.showSnackBar(SnackBar(content: Text(json['message']))); + debugPrint('API error: ' + json['message']); + } catch (e) { + debugPrint(e.toString()); + messenger?.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})'))); } return null; } diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index 8c36021..b08790b 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -33,6 +33,7 @@ class _LoginPageState extends State { final prefs = await SharedPreferences.getInstance(); prefs.setString('token', jsonUser.token); prefs.setString('id', jsonUser.id); + prefs.setString('refresh-token', jsonUser.refreshToken); if (mounted) { ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged in'))); diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index d3f8bd9..32ba677 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -5,6 +5,7 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; import 'package:mobile/favorites.dart'; import 'package:mobile/register.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'login.dart'; import 'base/sidemenu.dart'; import 'profile.dart'; @@ -12,7 +13,14 @@ import 'api.dart' as api; import 'models.dart'; import 'package:http/http.dart' as http; -void main() { +void main() async { + final prefs = await SharedPreferences.getInstance(); + debugPrint('token: ' + (prefs.getString('token') ?? '') + ' reffresshh: ' + (prefs.getString("refresh-token") ?? '')); + if (prefs.getString("token") != null && prefs.getString("refresh-token") != null) { + final token = await api.request(null, api.ApiService.auth, "POST", "/RefreshToken", {'refresh-token': prefs.getString("refresh-token")}); + debugPrint(token); + } + runApp(const MyApp()); } diff --git a/Mobile/lib/models.dart b/Mobile/lib/models.dart index 78fb075..e9d4ba1 100644 --- a/Mobile/lib/models.dart +++ b/Mobile/lib/models.dart @@ -23,13 +23,15 @@ class Favorite { class Login { String token; String id; + String refreshToken; - Login(this.token, this.id); + Login(this.token, this.id, this.refreshToken); factory Login.fromJson(Map json) { return Login( json['token'], json['id'], + json['refreshToken'], ); } }