diff --git a/uni/lib/controller/networking/network_router.dart b/uni/lib/controller/networking/network_router.dart index c6bba5b61..f92b85e7b 100644 --- a/uni/lib/controller/networking/network_router.dart +++ b/uni/lib/controller/networking/network_router.dart @@ -7,6 +7,7 @@ import 'package:logger/logger.dart'; import 'package:synchronized/synchronized.dart'; import 'package:uni/controller/local_storage/app_shared_preferences.dart'; import 'package:uni/model/entities/session.dart'; +import 'package:uni/view/navigation_service.dart'; extension UriString on String { /// Converts a [String] to an [Uri]. @@ -16,13 +17,9 @@ extension UriString on String { /// Manages the networking of the app. class NetworkRouter { static http.Client? httpClient; - static const int loginRequestTimeout = 20; - static Lock loginLock = Lock(); - static Function onReloginFail = () {}; - /// Creates an authenticated [Session] on the given [faculty] with the /// given username [user] and password [pass]. static Future login(String user, String pass, List faculties, @@ -52,7 +49,7 @@ class NetworkRouter { } /// Determines if a re-login with the [session] is possible. - static Future relogin(Session session) { + static Future reLogin(Session session) { return loginLock.synchronized(() async { if (!session.persistentSession) { return false; @@ -94,10 +91,11 @@ class NetworkRouter { /// Returns the response body of the login in Sigarra /// given username [user] and password [pass]. - static Future loginInSigarra(String user, String pass, List faculties) async { + static Future loginInSigarra( + String user, String pass, List faculties) async { final String url = '${NetworkRouter.getBaseUrls(faculties)[0]}vld_validacao.validacao'; - + final response = await http.post(url.toUri(), body: { 'p_user': user, 'p_pass': pass @@ -149,12 +147,12 @@ class NetworkRouter { return response; } else if (response.statusCode == 403 && !(await userLoggedIn(session))) { // HTTP403 - Forbidden - final bool reLoginSuccessful = await relogin(session); + final bool reLoginSuccessful = await reLogin(session); if (reLoginSuccessful) { headers['cookie'] = session.cookies; return http.get(url.toUri(), headers: headers); } else { - onReloginFail(); + NavigationService.logout(); Logger().e('Login failed'); return Future.error('Login failed'); } diff --git a/uni/lib/controller/on_start_up.dart b/uni/lib/controller/on_start_up.dart deleted file mode 100644 index f51e5a032..000000000 --- a/uni/lib/controller/on_start_up.dart +++ /dev/null @@ -1,18 +0,0 @@ -import 'package:uni/controller/networking/network_router.dart'; -import 'package:uni/model/providers/startup/session_provider.dart'; -import 'package:uni/view/navigation_service.dart'; - -class OnStartUp { - static onStart(SessionProvider sessionProvider) { - setHandleReloginFail(sessionProvider); - } - - static setHandleReloginFail(SessionProvider sessionProvider) { - NetworkRouter.onReloginFail = () { - if (!sessionProvider.session.persistentSession) { - return NavigationService.logout(); - } - return Future.value(); - }; - } -} diff --git a/uni/lib/main.dart b/uni/lib/main.dart index 3b5039fe7..03248f59f 100644 --- a/uni/lib/main.dart +++ b/uni/lib/main.dart @@ -7,7 +7,6 @@ import 'package:provider/provider.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:uni/controller/background_workers/background_callback.dart'; import 'package:uni/controller/local_storage/app_shared_preferences.dart'; -import 'package:uni/controller/on_start_up.dart'; import 'package:uni/model/providers/lazy/bus_stop_provider.dart'; import 'package:uni/model/providers/lazy/calendar_provider.dart'; import 'package:uni/model/providers/lazy/exam_provider.dart'; @@ -57,7 +56,6 @@ Future main() async { FacultyLocationsProvider(), HomePageProvider()); - OnStartUp.onStart(stateProviders.sessionProvider); WidgetsFlutterBinding.ensureInitialized(); await Workmanager().initialize(workerStartCallback, diff --git a/uni/lib/model/providers/startup/profile_provider.dart b/uni/lib/model/providers/startup/profile_provider.dart index bcb7f1984..f2e48e0dd 100644 --- a/uni/lib/model/providers/startup/profile_provider.dart +++ b/uni/lib/model/providers/startup/profile_provider.dart @@ -13,6 +13,7 @@ import 'package:uni/controller/local_storage/app_courses_database.dart'; import 'package:uni/controller/local_storage/app_refresh_times_database.dart'; import 'package:uni/controller/local_storage/app_shared_preferences.dart'; import 'package:uni/controller/local_storage/app_user_database.dart'; +import 'package:uni/controller/local_storage/file_offline_storage.dart'; import 'package:uni/controller/parsers/parser_fees.dart'; import 'package:uni/controller/parsers/parser_print_balance.dart'; import 'package:uni/model/entities/course.dart'; @@ -22,8 +23,6 @@ import 'package:uni/model/entities/session.dart'; import 'package:uni/model/providers/state_provider_notifier.dart'; import 'package:uni/model/request_status.dart'; -import '../../../controller/local_storage/file_offline_storage.dart'; - class ProfileProvider extends StateProviderNotifier { Profile _profile = Profile(); DateTime? _feesRefreshTime; diff --git a/uni/lib/model/providers/startup/session_provider.dart b/uni/lib/model/providers/startup/session_provider.dart index 296f70ccc..b860c267e 100644 --- a/uni/lib/model/providers/startup/session_provider.dart +++ b/uni/lib/model/providers/startup/session_provider.dart @@ -11,6 +11,7 @@ import 'package:uni/model/entities/profile.dart'; import 'package:uni/model/entities/session.dart'; import 'package:uni/model/providers/state_provider_notifier.dart'; import 'package:uni/model/request_status.dart'; +import 'package:uni/view/navigation_service.dart'; class SessionProvider extends StateProviderNotifier { Session _session = Session(); @@ -47,7 +48,6 @@ class SessionProvider extends StateProviderNotifier { () => {NotificationManager().initializeNotifications()}); await acceptTermsAndConditions(); - updateStatus(RequestStatus.successful); } else { final String responseHtml = @@ -81,7 +81,7 @@ class SessionProvider extends StateProviderNotifier { updateStatus(RequestStatus.successful); action?.complete(); } else { - failReLogin(action); + handleFailedReLogin(action); } } catch (e) { _session = Session( @@ -92,14 +92,14 @@ class SessionProvider extends StateProviderNotifier { cookies: '', persistentSession: true); - failReLogin(action); + handleFailedReLogin(action); } } - void failReLogin(Completer? action) { - notifyListeners(); - updateStatus(RequestStatus.failed); + handleFailedReLogin(Completer? action) { action?.completeError(RequestStatus.failed); - NetworkRouter.onReloginFail(); + if (!session.persistentSession) { + return NavigationService.logout(); + } } }