Skip to content

Commit c78c2c4

Browse files
Merge pull request #120 from PennyPilot/fix/initial-screen
Fix/initial screen
2 parents 28a6510 + 6fad685 commit c78c2c4

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

lib/money_app.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ class AuthRedirectNotifier extends ChangeNotifier {
1818
final AuthenticationRepository _authRepository;
1919
late final StreamSubscription<AuthState> _subscription;
2020
bool _isPasswordRecovery = false;
21+
bool _isAuthenticated = false;
22+
23+
bool get isAuthenticated => _isAuthenticated;
2124

2225
AuthRedirectNotifier(this._authRepository) {
2326
_subscription = _authRepository.onAuthStateChange.listen((data) {
2427
if (data.event == AuthChangeEvent.passwordRecovery) {
2528
_isPasswordRecovery = true;
2629
notifyListeners();
30+
} else if (data.session != null) {
31+
_isAuthenticated = true;
32+
notifyListeners();
33+
} else {
34+
_isAuthenticated = false;
35+
notifyListeners();
2736
}
2837
});
2938
}
@@ -39,13 +48,18 @@ final _authRedirectNotifier =
3948
AuthRedirectNotifier(getIt<AuthenticationRepository>());
4049
final _router = GoRouter(
4150
routes: $appRoutes,
42-
initialLocation: '/login',
51+
initialLocation: _authRedirectNotifier.isAuthenticated ? RoutePaths.main : RoutePaths.login,
4352
refreshListenable: _authRedirectNotifier,
4453
redirect: (context, state) {
4554
if (_authRedirectNotifier._isPasswordRecovery) {
4655
_authRedirectNotifier._isPasswordRecovery = false;
47-
return '/update_password';
56+
return RoutePaths.forgetPassword;
4857
}
58+
59+
final loggingIn = state.matchedLocation == RoutePaths.login;
60+
if (!_authRedirectNotifier.isAuthenticated && !loggingIn) return RoutePaths.login;
61+
if (_authRedirectNotifier.isAuthenticated && loggingIn) return RoutePaths.main;
62+
4963
return null;
5064
},
5165
);

lib/presentation/navigation/routes.dart

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@ import '../trasnaction_details/transaction_details_screen.dart';
1717

1818
part 'routes.g.dart';
1919

20-
@TypedGoRoute<OnBoardingRoute>(path: '/')
20+
abstract class RoutePaths {
21+
static const String onBoarding = '/';
22+
static const String login = '/login';
23+
static const String main = '/main';
24+
static const String createAccount = '/createAccount';
25+
static const String statistics = '/statistics';
26+
static const String transactionDetails = '/transaction_details';
27+
static const String forgetPassword = '/forget_password';
28+
static const String updatePassword = '/update_password';
29+
static const String addIncome = '/add-income';
30+
static const String addExpense = '/add-expense';
31+
}
32+
33+
@TypedGoRoute<OnBoardingRoute>(path: RoutePaths.onBoarding)
2134
@immutable
2235
class OnBoardingRoute extends GoRouteData with $OnBoardingRoute {
2336
const OnBoardingRoute();
@@ -38,7 +51,7 @@ class OnBoardingRoute extends GoRouteData with $OnBoardingRoute {
3851
}
3952
}
4053

41-
@TypedGoRoute<LoginRoute>(path: '/login')
54+
@TypedGoRoute<LoginRoute>(path: RoutePaths.login)
4255
@immutable
4356
class LoginRoute extends GoRouteData with $LoginRoute {
4457
const LoginRoute();
@@ -52,7 +65,7 @@ class LoginRoute extends GoRouteData with $LoginRoute {
5265
}
5366
}
5467

55-
@TypedGoRoute<MainRoute>(path: '/main')
68+
@TypedGoRoute<MainRoute>(path: RoutePaths.main)
5669
@immutable
5770
class MainRoute extends GoRouteData with $MainRoute {
5871
const MainRoute();
@@ -63,7 +76,7 @@ class MainRoute extends GoRouteData with $MainRoute {
6376
}
6477
}
6578

66-
@TypedGoRoute<CreateAccountRoute>(path: '/createAccount')
79+
@TypedGoRoute<CreateAccountRoute>(path: RoutePaths.createAccount)
6780
@immutable
6881
class CreateAccountRoute extends GoRouteData
6982
with $CreateAccountRoute {
@@ -75,7 +88,7 @@ class CreateAccountRoute extends GoRouteData
7588
}
7689
}
7790

78-
@TypedGoRoute<StatisticsRoute>(path: '/statistics')
91+
@TypedGoRoute<StatisticsRoute>(path: RoutePaths.statistics)
7992
@immutable
8093
class StatisticsRoute extends GoRouteData
8194
with $StatisticsRoute {
@@ -90,7 +103,7 @@ class StatisticsRoute extends GoRouteData
90103
}
91104
}
92105

93-
@TypedGoRoute<TransactionDetailsRoute>(path: '/transaction_details')
106+
@TypedGoRoute<TransactionDetailsRoute>(path: RoutePaths.transactionDetails)
94107
@immutable
95108
class TransactionDetailsRoute extends GoRouteData with $TransactionDetailsRoute {
96109
final String transactionId;
@@ -102,7 +115,7 @@ class TransactionDetailsRoute extends GoRouteData with $TransactionDetailsRoute
102115
}
103116
}
104117

105-
@TypedGoRoute<ForgetPasswordRoute>(path: '/forget_password')
118+
@TypedGoRoute<ForgetPasswordRoute>(path: RoutePaths.forgetPassword)
106119
@immutable
107120
class ForgetPasswordRoute extends GoRouteData with $ForgetPasswordRoute {
108121
const ForgetPasswordRoute();
@@ -113,7 +126,7 @@ class ForgetPasswordRoute extends GoRouteData with $ForgetPasswordRoute {
113126
}
114127
}
115128

116-
@TypedGoRoute<UpdatePasswordRoute>(path: '/update_password')
129+
@TypedGoRoute<UpdatePasswordRoute>(path: RoutePaths.updatePassword)
117130
@immutable
118131
class UpdatePasswordRoute extends GoRouteData with $UpdatePasswordRoute {
119132
@override
@@ -122,7 +135,7 @@ class UpdatePasswordRoute extends GoRouteData with $UpdatePasswordRoute {
122135
}
123136
}
124137

125-
@TypedGoRoute<AddIncomeRoute>(path: '/add-income')
138+
@TypedGoRoute<AddIncomeRoute>(path: RoutePaths.addIncome)
126139
@immutable
127140
class AddIncomeRoute extends GoRouteData with $AddIncomeRoute {
128141
const AddIncomeRoute();
@@ -133,7 +146,7 @@ class AddIncomeRoute extends GoRouteData with $AddIncomeRoute {
133146
}
134147
}
135148

136-
@TypedGoRoute<AddExpenseRoute>(path: '/add-expense')
149+
@TypedGoRoute<AddExpenseRoute>(path: RoutePaths.addExpense)
137150
@immutable
138151
class AddExpenseRoute extends GoRouteData with $AddExpenseRoute {
139152
const AddExpenseRoute();

0 commit comments

Comments
 (0)