Skip to content

Commit c261e71

Browse files
committed
refactor(auth): improve linking context handling
- Preserve linking context in auth flow - Prevent redirect in linking context
1 parent a4f6467 commit c261e71

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

lib/authentication/view/request_code_page.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class _RequestCodeView extends StatelessWidget {
6060
// If linking, go back to Auth page preserving the linking query param.
6161
context.goNamed(
6262
Routes.authenticationName,
63-
queryParameters: {'context': 'linking'},
63+
queryParameters:
64+
isLinkingContext ? {'context': 'linking'} : const {},
6465
);
6566
} else {
6667
// If normal sign-in, just go back to the Auth page.
@@ -86,6 +87,8 @@ class _RequestCodeView extends StatelessWidget {
8687
context.goNamed(
8788
Routes.verifyCodeName,
8889
pathParameters: {'email': state.email},
90+
queryParameters:
91+
isLinkingContext ? {'context': 'linking'} : const {},
8992
);
9093
}
9194
},

lib/router/router.dart

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,30 @@ GoRouter createRouter({
135135
appStatus == AppStatus.authenticated) {
136136
print(' Redirect Decision: User is $appStatus.');
137137

138-
final isLinkingContext =
139-
currentUri.queryParameters['context'] == 'linking';
138+
final isLinkingContext =
139+
state.uri.queryParameters['context'] == 'linking';
140140

141-
// If an authenticated/anonymous user is on any authentication-related path,
142-
// redirect them to the feed, unless it's the base authentication path
143-
// AND it's specifically for account linking.
144-
if (currentLocation.startsWith(authenticationPath) &&
145-
!(currentLocation == authenticationPath && isLinkingContext)) {
141+
// If an authenticated/anonymous user is on any authentication-related path:
142+
if (currentLocation.startsWith(authenticationPath)) {
143+
// Allow navigation within auth paths if the linking context is active
144+
if (isLinkingContext) {
145+
print(
146+
' Action: $appStatus user on auth path ($currentLocation) with linking context. Allowing navigation.',
147+
);
148+
return null;
149+
} else {
150+
// Redirect to feed if not in a linking context (e.g., user manually types /authentication)
151+
print(
152+
' Action: $appStatus user trying to access an auth path ($currentLocation) without linking context. Redirecting to $feedPath',
153+
);
154+
return feedPath;
155+
}
156+
}
157+
// Allow access to other routes (non-auth paths)
146158
print(
147-
' Action: $appStatus user trying to access an auth path. Redirecting to $feedPath',
159+
' Action: Allowing navigation to $currentLocation for $appStatus user (non-auth path).',
148160
);
149-
return feedPath;
150-
}
151-
152-
// Allow access to other routes (only if not an auth path, or if it's the base auth path for linking)
153-
print(
154-
' Action: Allowing navigation to $currentLocation for $appStatus user.',
155-
);
156-
return null;
161+
return null;
157162
}
158163

159164
// Fallback (should ideally not be reached if all statuses are handled)
@@ -733,7 +738,7 @@ GoRouter createRouter({
733738
.read<
734739
HtDataRepository<Headline>
735740
>(),
736-
),
741+
),
737742
),
738743
BlocProvider(
739744
create:

0 commit comments

Comments
 (0)