diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index f1d7ea93..d2843ed5 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -172,18 +172,10 @@ class _AuthNotifier extends ChangeNotifier { /// Logs out the user, also sends this info to server Future logout() async { - // 1) Clear user's storage first so, - // if the logout fails in the steps below - // he can still login - // 2) Sign-out from google - // 3) Notify backend about logout - // 4) remove user, player, token from provider - - // clear values from the database and utilities - await utilities.Database.clear(); - - utilities.api.options.headers["authorization"] = null; - utilities.Global.isAuthenticated = false; + // 1) Sign-out from google + // 2) Notify backend about logout + // 3) remove user, player, token from provider + // 4) Clear user's storage try { await GoogleSignIn().signOut(); @@ -205,6 +197,12 @@ class _AuthNotifier extends ChangeNotifier { ref.read(matches_provider.matches).clearData(); ref.read(players_provider.players).clearData(); + // clear values from the database and utilities + await utilities.Database.clear(); + + utilities.api.options.headers["authorization"] = null; + utilities.Global.isAuthenticated = false; + return true; } diff --git a/lib/screens/login/login.dart b/lib/screens/login/login.dart index 8a26e56e..f9bcde81 100644 --- a/lib/screens/login/login.dart +++ b/lib/screens/login/login.dart @@ -16,6 +16,7 @@ class Login extends HookConsumerWidget { name: routeName, path: routePath, builder: _routeBuilder, + redirect: _routeRedirect, ); const Login({Key? key}) : super(key: key); @@ -98,4 +99,12 @@ class Login extends HookConsumerWidget { } static Login _routeBuilder(_, __) => const Login(); + + static String? _routeRedirect(GoRouterState _) { + // check if user is authenticated + // send him to main if authenticated + if (utilities.Global.isAuthenticated) return screens.Main.routePath; + + return null; + } } diff --git a/lib/screens/main/main.dart b/lib/screens/main/main.dart index 37245f5a..660d3129 100644 --- a/lib/screens/main/main.dart +++ b/lib/screens/main/main.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:go_router/go_router.dart'; import 'package:paladinsedge/screens/app_drawer/index.dart'; +import 'package:paladinsedge/screens/index.dart' as screens; import 'package:paladinsedge/screens/main/main_bottom_tabs.dart'; import 'package:paladinsedge/screens/main/main_pages_stack.dart'; import 'package:paladinsedge/utilities/index.dart' as utilities; @@ -10,7 +11,6 @@ class Main extends HookWidget { static const routeName = 'main'; static const routePath = '/'; final int startIndex; - const Main({ this.startIndex = 0, Key? key, @@ -21,6 +21,7 @@ class Main extends HookWidget { path: routePath, builder: _routeBuilder, routes: routes, + redirect: _routeRedirect, ); @override @@ -56,4 +57,16 @@ class Main extends HookWidget { } static Main _routeBuilder(_, __) => const Main(startIndex: 0); + + static String? _routeRedirect(GoRouterState _) { + if (utilities.Global.isInitialRoute && !utilities.Global.isAuthenticated) { + utilities.Global.isInitialRoute = false; + + return screens.Login.routePath; + } else { + utilities.Global.isInitialRoute = false; + } + + return null; + } } diff --git a/lib/utilities/global.dart b/lib/utilities/global.dart index 8fe6af17..7968c61b 100644 --- a/lib/utilities/global.dart +++ b/lib/utilities/global.dart @@ -16,4 +16,7 @@ abstract class Global { /// If user is logged in and player=null, /// then navigate to connectProfile static bool isPlayerConnected = false; + + /// used to detect if the route is being accessed initially + static bool isInitialRoute = true; } diff --git a/pubspec.yaml b/pubspec.yaml index 5c079d2a..e5337b5f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 0.18.1 +version: 0.18.2 homepage: "https://paladinsedge.app"