@@ -135,23 +135,47 @@ GoRouter createRouter({
135135 appStatus == AppStatus .authenticated) {
136136 print (' Redirect Decision: User is $appStatus .' );
137137
138- final isLinkingContext =
139- currentUri.queryParameters[ 'context' ] == ' linking' ;
138+ final isLinkingContextQueryPresent = state.uri.queryParameters[ 'context' ] == 'linking' ;
139+ final isLinkingPathSegmentPresent = currentLocation. contains ( '/ linking/' ) ;
140140
141- // If an authenticated/anonymous user tries to access the BASE /authentication path
142- // AND it's NOT for account linking, redirect them to the feed.
143- if (currentLocation == authenticationPath && ! isLinkingContext) {
141+ // Determine if the current location is part of any linking flow (either via query or path segment)
142+ final isAnyLinkingContext = isLinkingContextQueryPresent || isLinkingPathSegmentPresent;
143+
144+ // If an authenticated/anonymous user is on any authentication-related path:
145+ if (currentLocation.startsWith (authenticationPath)) {
146+ print (' Debug: Auth path detected. Current Location: $currentLocation ' );
147+ print (' Debug: URI Query Parameters: ${state .uri .queryParameters }' );
148+ print (' Debug: isLinkingContextQueryPresent: $isLinkingContextQueryPresent ' );
149+ print (' Debug: isLinkingPathSegmentPresent: $isLinkingPathSegmentPresent ' );
150+ print (' Debug: isAnyLinkingContext evaluated to: $isAnyLinkingContext ' );
151+
152+ // If the user is authenticated, always redirect away from auth paths.
153+ if (appStatus == AppStatus .authenticated) {
154+ print (
155+ ' Action: Authenticated user on auth path ($currentLocation ). Redirecting to $feedPath ' ,
156+ );
157+ return feedPath;
158+ }
159+
160+ // If the user is anonymous, allow navigation within auth paths if in a linking context.
161+ // Otherwise, redirect anonymous users trying to access non-linking auth paths to feed.
162+ if (isAnyLinkingContext) {
163+ print (
164+ ' Action: Anonymous user on auth linking path ($currentLocation ). Allowing navigation.' ,
165+ );
166+ return null ;
167+ } else {
168+ print (
169+ ' Action: Anonymous user trying to access non-linking auth path ($currentLocation ). Redirecting to $feedPath ' ,
170+ );
171+ return feedPath;
172+ }
173+ }
174+ // Allow access to other routes (non-auth paths)
144175 print (
145- ' Action: $ appStatus user trying to access base auth path without linking context. Redirecting to $ feedPath ' ,
176+ ' Action: Allowing navigation to $ currentLocation for $ appStatus user (non-auth path). ' ,
146177 );
147- return feedPath;
148- }
149-
150- // Allow access to other routes (including auth sub-routes if linking, or any other app route)
151- print (
152- ' Action: Allowing navigation to $currentLocation for $appStatus user.' ,
153- );
154- return null ;
178+ return null ;
155179 }
156180
157181 // Fallback (should ideally not be reached if all statuses are handled)
@@ -200,24 +224,41 @@ GoRouter createRouter({
200224 );
201225 },
202226 routes: [
227+ // Nested route for account linking flow (defined first for priority)
203228 GoRoute (
204- path: Routes .requestCode, // Use new path
205- name: Routes .requestCodeName, // Use new name
206- builder: (context, state) {
207- // Extract the linking context flag from 'extra', default to false.
208- final isLinking = (state.extra as bool ? ) ?? false ;
209- return RequestCodePage (isLinkingContext: isLinking);
210- },
229+ path: Routes .accountLinking, // This is 'linking'
230+ name: Routes .accountLinkingName, // Name for the linking segment
231+ builder: (context, state) => const SizedBox .shrink (), // Placeholder
232+ routes: [
233+ GoRoute (
234+ path: Routes .requestCode, // Path: /authentication/linking/request-code
235+ name: Routes .linkingRequestCodeName,
236+ builder: (context, state) =>
237+ const RequestCodePage (isLinkingContext: true ),
238+ ),
239+ GoRoute (
240+ path: '${Routes .verifyCode }/:email' , // Path: /authentication/linking/verify-code/:email
241+ name: Routes .linkingVerifyCodeName,
242+ builder: (context, state) {
243+ final email = state.pathParameters['email' ]! ;
244+ return EmailCodeVerificationPage (email: email);
245+ },
246+ ),
247+ ],
211248 ),
249+ // Non-linking authentication routes (defined after linking routes)
212250 GoRoute (
213- path:
214- '${Routes .verifyCode }/:email' , // Use new path with email parameter
215- name: Routes .verifyCodeName, // Use new name
251+ path: Routes .requestCode,
252+ name: Routes .requestCodeName,
253+ builder: (context, state) =>
254+ const RequestCodePage (isLinkingContext: false ),
255+ ),
256+ GoRoute (
257+ path: '${Routes .verifyCode }/:email' ,
258+ name: Routes .verifyCodeName,
216259 builder: (context, state) {
217- final email = state.pathParameters['email' ]! ; // Extract email
218- return EmailCodeVerificationPage (
219- email: email,
220- ); // Use renamed page
260+ final email = state.pathParameters['email' ]! ;
261+ return EmailCodeVerificationPage (email: email);
221262 },
222263 ),
223264 ],
@@ -731,7 +772,7 @@ GoRouter createRouter({
731772 .read<
732773 HtDataRepository <Headline >
733774 > (),
734- ),
775+ ),
735776 ),
736777 BlocProvider (
737778 create:
0 commit comments