Skip to content

Commit e4e390c

Browse files
committed
refactor(router): simplify redirect logic in GoRouter
- Remove unnecessary checks for app config and URI - Consolidate print statements and remove detailed debug logs - Simplify unauthenticated user redirect logic - Reduce complexity in anonymous/authenticated user handling - Remove fallback case, as all statuses should be handled
1 parent 8486354 commit e4e390c

File tree

1 file changed

+31
-88
lines changed

1 file changed

+31
-88
lines changed

lib/router/router.dart

Lines changed: 31 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -78,125 +78,68 @@ GoRouter createRouter({
7878
// --- Redirect Logic ---
7979
redirect: (BuildContext context, GoRouterState state) {
8080
final appStatus = context.read<AppBloc>().state.status;
81-
final appConfig = context.read<AppBloc>().state.remoteConfig;
8281
final currentLocation = state.matchedLocation;
83-
final currentUri = state.uri;
8482

8583
print(
8684
'GoRouter Redirect Check:\n'
8785
' Current Location (Matched): $currentLocation\n'
88-
' Current URI (Full): $currentUri\n'
89-
' AppStatus: $appStatus\n'
90-
' AppConfig isNull: ${appConfig == null}',
86+
' AppStatus: $appStatus',
9187
);
9288

9389
// --- Define Key Paths ---
9490
const authenticationPath = Routes.authentication;
9591
const feedPath = Routes.feed;
9692
final isGoingToAuth = currentLocation.startsWith(authenticationPath);
9793

98-
// --- Case 0: App is Initializing or Config is being fetched/failed ---
99-
if (appStatus == AppStatus.initial ||
100-
appStatus == AppStatus.configFetching ||
101-
appStatus == AppStatus.configFetchFailed) {
102-
// If AppStatus is initial and trying to go to a non-auth page (e.g. initial /feed)
103-
// redirect to auth immediately to settle auth status first.
104-
if (appStatus == AppStatus.initial && !isGoingToAuth) {
105-
print(
106-
' Redirect Decision: AppStatus is INITIAL and not going to auth. Redirecting to $authenticationPath to settle auth first.',
107-
);
108-
return authenticationPath;
109-
}
110-
// For configFetching or configFetchFailed, or initial going to auth,
111-
// let the App widget's builder handle the UI (loading/error screen).
112-
print(
113-
' Redirect Decision: AppStatus is $appStatus. Allowing App widget to handle display or navigation to auth.',
114-
);
115-
return null;
116-
}
94+
// With the new App startup architecture, the router is only active when
95+
// the app is in a stable, running state. The `redirect` function's
96+
// only responsibility is to handle auth-based route protection.
97+
// States like `configFetching`, `underMaintenance`, etc., are now
98+
// handled by the root App widget *before* this router is ever built.
11799

118-
// --- Case 1: Unauthenticated User (after initial phase, config not relevant yet for this decision) ---
100+
// --- Case 1: Unauthenticated User ---
101+
// If the user is unauthenticated, they should be on an auth path.
102+
// If they are trying to access any other part of the app, redirect them.
119103
if (appStatus == AppStatus.unauthenticated) {
120-
print(' Redirect Decision: User is UNauthenticated.');
121-
if (!isGoingToAuth) {
122-
print(
123-
' Action: Not going to auth. Redirecting to $authenticationPath',
124-
);
125-
return authenticationPath;
126-
}
127-
print(' Action: Already going to auth. Allowing navigation.');
128-
return null;
104+
print(' Redirect: User is unauthenticated.');
105+
// If they are already on an auth path, allow it. Otherwise, redirect.
106+
return isGoingToAuth ? null : authenticationPath;
129107
}
130108

131109
// --- Case 2: Anonymous or Authenticated User ---
132-
// (Covers AppStatus.anonymous and AppStatus.authenticated)
133-
// At this point, AppConfig should be loaded or its loading/error state is handled by App widget.
134-
// The main concern here is preventing authenticated users from re-entering basic auth flows.
110+
// If a user is anonymous or authenticated, they should not be able to
111+
// access the main authentication flows, with an exception for account
112+
// linking for anonymous users.
135113
if (appStatus == AppStatus.anonymous ||
136114
appStatus == AppStatus.authenticated) {
137-
print(' Redirect Decision: User is $appStatus.');
115+
print(' Redirect: User is $appStatus.');
138116

139-
final isLinkingContextQueryPresent =
140-
state.uri.queryParameters['context'] == 'linking';
141-
final isLinkingPathSegmentPresent = currentLocation.contains(
142-
'/linking/',
143-
);
144-
145-
// Determine if the current location is part of any linking flow (either via query or path segment)
146-
final isAnyLinkingContext =
147-
isLinkingContextQueryPresent || isLinkingPathSegmentPresent;
148-
149-
// If an authenticated/anonymous user is on any authentication-related path:
150-
if (currentLocation.startsWith(authenticationPath)) {
151-
print(
152-
' Debug: Auth path detected. Current Location: $currentLocation',
153-
);
154-
print(
155-
' Debug: URI Query Parameters: ${state.uri.queryParameters}',
156-
);
157-
print(
158-
' Debug: isLinkingContextQueryPresent: $isLinkingContextQueryPresent',
159-
);
160-
print(
161-
' Debug: isLinkingPathSegmentPresent: $isLinkingPathSegmentPresent',
162-
);
163-
print(
164-
' Debug: isAnyLinkingContext evaluated to: $isAnyLinkingContext',
165-
);
166-
167-
// If the user is authenticated, always redirect away from auth paths.
117+
// If the user is trying to access an authentication path:
118+
if (isGoingToAuth) {
119+
// A fully authenticated user should never see auth pages.
168120
if (appStatus == AppStatus.authenticated) {
169-
print(
170-
' Action: Authenticated user on auth path ($currentLocation). Redirecting to $feedPath',
171-
);
121+
print(' Action: Authenticated user on auth path. Redirecting to feed.');
172122
return feedPath;
173123
}
174124

175-
// If the user is anonymous, allow navigation within auth paths if in a linking context.
176-
// Otherwise, redirect anonymous users trying to access non-linking auth paths to feed.
177-
if (isAnyLinkingContext) {
178-
print(
179-
' Action: Anonymous user on auth linking path ($currentLocation). Allowing navigation.',
180-
);
125+
// An anonymous user is only allowed on auth paths for account linking.
126+
final isLinking =
127+
state.uri.queryParameters['context'] == 'linking' ||
128+
currentLocation.contains('/linking/');
129+
130+
if (isLinking) {
131+
print(' Action: Anonymous user on linking path. Allowing.');
181132
return null;
182133
} else {
183-
print(
184-
' Action: Anonymous user trying to access non-linking auth path ($currentLocation). Redirecting to $feedPath',
185-
);
134+
print(' Action: Anonymous user on non-linking auth path. Redirecting to feed.');
186135
return feedPath;
187136
}
188137
}
189-
// Allow access to other routes (non-auth paths)
190-
print(
191-
' Action: Allowing navigation to $currentLocation for $appStatus user (non-auth path).',
192-
);
193-
return null;
194138
}
195139

196-
// Fallback (should ideally not be reached if all statuses are handled)
197-
print(
198-
' Redirect Decision: Fallback, no specific condition met for $appStatus. Allowing navigation.',
199-
);
140+
// --- Fallback ---
141+
// For any other case, allow navigation.
142+
print(' Redirect: No condition met. Allowing navigation.');
200143
return null;
201144
},
202145
// --- Authentication Routes ---

0 commit comments

Comments
 (0)