From 5b38559e1c764ac2caa420a4241d2ccf526e323b Mon Sep 17 00:00:00 2001 From: LilleBRG Date: Thu, 22 Aug 2024 08:51:13 +0200 Subject: [PATCH] fixed api requests from sidemenu --- Mobile/lib/api.dart | 20 +++++++++++++++----- Mobile/lib/login.dart | 9 +++------ Mobile/lib/register.dart | 9 ++------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 4a4276c..7a4e6a8 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -2,13 +2,16 @@ import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; +import 'dart:developer'; enum ApiService { auth, app, } -Future request(BuildContext context, ApiService service, String method, String path, Object? body) async { +Future request(BuildContext context, ApiService service, String method, + String path, Object? body) async { + log('hello'); final messenger = ScaffoldMessenger.of(context); final host = switch (service) { @@ -16,6 +19,9 @@ Future request(BuildContext context, ApiService service, String method, ApiService.app => const String.fromEnvironment('APP_SERVICE_HOST'), }; + log('hello'); + log(const String.fromEnvironment('AUTH_SERVICE_HOST')); + final http.Response response; try { @@ -36,7 +42,8 @@ Future request(BuildContext context, ApiService service, String method, ); } } catch (_) { - messenger.showSnackBar(const SnackBar(content: Text('Unable to connect to server'))); + messenger.showSnackBar( + const SnackBar(content: Text('Unable to connect to server'))); return null; } @@ -45,7 +52,8 @@ Future request(BuildContext context, ApiService service, String method, 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('Something went wrong (HTTP ${response.statusCode})'))); } return null; } @@ -67,12 +75,14 @@ Future isLoggedIn(BuildContext context) async { final payload = jsonDecode(String.fromCharCodes(base64Decode(base64))); if (payload['exp'] < DateTime.now().millisecondsSinceEpoch / 1000) { - messenger.showSnackBar(const SnackBar(content: Text('Token expired, please sign in again'))); + messenger.showSnackBar( + const SnackBar(content: Text('Token expired, please sign in again'))); prefs.remove('token'); return false; } } catch (e) { - messenger.showSnackBar(const SnackBar(content: Text('Invalid token, please sign in again'))); + messenger.showSnackBar( + const SnackBar(content: Text('Invalid token, please sign in again'))); prefs.remove('token'); return false; } diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index 13b6f46..224b905 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:mobile/base/sidemenu.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'register.dart'; import 'api.dart' as api; class LoginPage extends StatefulWidget { @@ -30,7 +29,7 @@ class _LoginPageState extends State { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Successfully logged in'))); - Navigator.pop(context); + Navigator.pushReplacementNamed(context, '/home'); } } @@ -57,10 +56,8 @@ class _LoginPageState extends State { const SizedBox(height: 10), TextButton( child: const Text('Register account'), - onPressed: () => Navigator.pushReplacement( - context, - MaterialPageRoute( - builder: (context) => const RegisterPage()))), + onPressed: () => + Navigator.pushReplacementNamed(context, '/register')), ]), ), ), diff --git a/Mobile/lib/register.dart b/Mobile/lib/register.dart index 2a92c34..6959ed0 100644 --- a/Mobile/lib/register.dart +++ b/Mobile/lib/register.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:mobile/base/sidemenu.dart'; -import 'login.dart'; import 'api.dart' as api; class RegisterPage extends StatefulWidget { @@ -28,8 +27,7 @@ class _RegisterPageState extends State { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(const SnackBar( content: Text('Successfully registered, please login'))); - Navigator.pushReplacement( - context, MaterialPageRoute(builder: (context) => const LoginPage())); + Navigator.pushReplacementNamed(context, '/login'); } } @@ -60,10 +58,7 @@ class _RegisterPageState extends State { const SizedBox(height: 10), TextButton( child: const Text('Login'), - onPressed: () => Navigator.pushReplacement( - context, - MaterialPageRoute( - builder: (context) => const LoginPage()))), + onPressed: () => Navigator.pushReplacementNamed(context, '/login')), ]), ), ),