Skip to content

Commit 73c16cc

Browse files
committed
fix: Ensures that when you navigate back from the EmailSignInPage, the AuthenticationPage is displayed with the correct context (either linking or initial sign-in) based on the flow you were originally in.
1 parent d518969 commit 73c16cc

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

lib/authentication/view/authentication_page.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ class _AuthenticationView extends StatelessWidget {
170170
isLoading
171171
? null
172172
: () {
173-
// Navigate to the dedicated email sign-in page
174-
context.goNamed(Routes.emailSignInName);
173+
// Navigate to the dedicated email sign-in page,
174+
// passing the linking context via 'extra'.
175+
context.goNamed(
176+
Routes.emailSignInName,
177+
extra: isLinkingContext, // Pass the flag
178+
);
175179
},
176180
),
177181

lib/authentication/view/email_sign_in_page.dart

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ import 'package:ht_main/shared/constants/app_spacing.dart';
1212
/// {@endtemplate}
1313
class EmailSignInPage extends StatelessWidget {
1414
/// {@macro email_sign_in_page}
15-
const EmailSignInPage({super.key});
15+
const EmailSignInPage({
16+
required this.isLinkingContext, // Accept the flag
17+
super.key,
18+
});
19+
20+
/// Whether this page is being shown in the account linking context.
21+
final bool isLinkingContext;
1622

1723
@override
1824
Widget build(BuildContext context) {
1925
// Assuming AuthenticationBloc is provided by the parent route (AuthenticationPage)
2026
// If not, it needs to be provided here or higher up.
21-
return const _EmailSignInView();
27+
// Pass the flag down to the view.
28+
return _EmailSignInView(isLinkingContext: isLinkingContext);
2229
}
2330
}
2431

2532
class _EmailSignInView extends StatelessWidget {
26-
const _EmailSignInView();
33+
// Accept the flag from the parent page.
34+
const _EmailSignInView({required this.isLinkingContext});
35+
36+
final bool isLinkingContext;
2737

2838
@override
2939
Widget build(BuildContext context) {
@@ -33,6 +43,24 @@ class _EmailSignInView extends StatelessWidget {
3343
return Scaffold(
3444
appBar: AppBar(
3545
title: Text(l10n.emailSignInPageTitle), // New l10n key needed
46+
// Add a custom leading back button to control navigation based on context.
47+
leading: IconButton(
48+
icon: const Icon(Icons.arrow_back),
49+
tooltip: MaterialLocalizations.of(context).backButtonTooltip, // Accessibility
50+
onPressed: () {
51+
// Navigate back differently based on the context.
52+
if (isLinkingContext) {
53+
// If linking, go back to Auth page preserving the linking query param.
54+
context.goNamed(
55+
Routes.authenticationName,
56+
queryParameters: {'context': 'linking'},
57+
);
58+
} else {
59+
// If normal sign-in, just go back to the Auth page.
60+
context.goNamed(Routes.authenticationName);
61+
}
62+
},
63+
),
3664
),
3765
body: SafeArea(
3866
child: BlocConsumer<AuthenticationBloc, AuthenticationState>(
@@ -52,10 +80,12 @@ class _EmailSignInView extends StatelessWidget {
5280
}
5381
},
5482
// BuildWhen prevents unnecessary rebuilds if only listening
55-
buildWhen: (previous, current) =>
56-
current is AuthenticationInitial ||
57-
current is AuthenticationLinkSending ||
58-
current is AuthenticationFailure, // Rebuild on failure to re-enable form
83+
buildWhen:
84+
(previous, current) =>
85+
current is AuthenticationInitial ||
86+
current is AuthenticationLinkSending ||
87+
current
88+
is AuthenticationFailure, // Rebuild on failure to re-enable form
5989
builder: (context, state) {
6090
final isLoading = state is AuthenticationLinkSending;
6191

@@ -110,10 +140,10 @@ class _EmailLinkFormState extends State<_EmailLinkForm> {
110140
void _submitForm() {
111141
if (_formKey.currentState!.validate()) {
112142
context.read<AuthenticationBloc>().add(
113-
AuthenticationSendSignInLinkRequested(
114-
email: _emailController.text.trim(),
115-
),
116-
);
143+
AuthenticationSendSignInLinkRequested(
144+
email: _emailController.text.trim(),
145+
),
146+
);
117147
}
118148
}
119149

@@ -140,22 +170,27 @@ class _EmailLinkFormState extends State<_EmailLinkForm> {
140170
validator: (value) {
141171
if (value == null || value.isEmpty || !value.contains('@')) {
142172
// Add a specific validation error message key if needed
143-
return l10n.accountLinkingEmailValidationError; // Re-use for now
173+
return l10n
174+
.accountLinkingEmailValidationError; // Re-use for now
144175
}
145176
return null;
146177
},
147-
onFieldSubmitted: (_) => _submitForm(), // Allow submitting from keyboard
178+
onFieldSubmitted:
179+
(_) => _submitForm(), // Allow submitting from keyboard
148180
),
149181
const SizedBox(height: AppSpacing.lg),
150182
ElevatedButton(
151183
onPressed: widget.isLoading ? null : _submitForm,
152-
child: widget.isLoading
153-
? const SizedBox(
154-
height: 24, // Consistent height
155-
width: 24,
156-
child: CircularProgressIndicator(strokeWidth: 2),
157-
)
158-
: Text(l10n.authenticationSendLinkButton), // Re-use existing key
184+
child:
185+
widget.isLoading
186+
? const SizedBox(
187+
height: 24, // Consistent height
188+
width: 24,
189+
child: CircularProgressIndicator(strokeWidth: 2),
190+
)
191+
: Text(
192+
l10n.authenticationSendLinkButton,
193+
), // Re-use existing key
159194
),
160195
],
161196
),

lib/router/router.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ GoRouter createRouter({required ValueNotifier<AppStatus> authStatusNotifier}) {
188188
GoRoute(
189189
path: Routes.emailSignIn,
190190
name: Routes.emailSignInName,
191-
builder: (context, state) => const EmailSignInPage(),
191+
builder: (context, state) {
192+
// Extract the linking context flag from 'extra', default to false.
193+
final isLinking = (state.extra as bool?) ?? false;
194+
return EmailSignInPage(isLinkingContext: isLinking); // Pass to widget
195+
},
192196
),
193197
GoRoute(
194198
path: Routes.emailLinkSent,

0 commit comments

Comments
 (0)