Skip to content

Commit

Permalink
fix(navigation): add ability to redirect from login and main
Browse files Browse the repository at this point in the history
If user tries to access /login or / link directly, we will first be checking for their account
creation

resolves #287
  • Loading branch information
tusharlock10 committed May 13, 2022
1 parent bbeabd3 commit 72ea7a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
22 changes: 10 additions & 12 deletions lib/providers/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,10 @@ class _AuthNotifier extends ChangeNotifier {

/// Logs out the user, also sends this info to server
Future<bool> 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();
Expand All @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions lib/screens/login/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Login extends HookConsumerWidget {
name: routeName,
path: routePath,
builder: _routeBuilder,
redirect: _routeRedirect,
);

const Login({Key? key}) : super(key: key);
Expand Down Expand Up @@ -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;
}
}
15 changes: 14 additions & 1 deletion lib/screens/main/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -21,6 +21,7 @@ class Main extends HookWidget {
path: routePath,
builder: _routeBuilder,
routes: routes,
redirect: _routeRedirect,
);

@override
Expand Down Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions lib/utilities/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 72ea7a5

Please sign in to comment.