Skip to content

Fix misc auth bugs #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/authentication/bloc/authentication_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AuthenticationBloc
: _authenticationRepository = authenticationRepository,
super(AuthenticationInitial()) {
// Listen to authentication state changes from the repository
_authenticationRepository.authStateChanges.listen(
_userAuthSubscription = _authenticationRepository.authStateChanges.listen(
(user) => add(_AuthenticationUserChanged(user: user)),
);

Expand All @@ -42,6 +42,7 @@ class AuthenticationBloc
}

final HtAuthRepository _authenticationRepository;
late final StreamSubscription<User?> _userAuthSubscription;

/// Handles [_AuthenticationUserChanged] events.
Future<void> _onAuthenticationUserChanged(
Expand Down Expand Up @@ -83,7 +84,8 @@ class AuthenticationBloc
emit(AuthenticationFailure('Operation failed: ${e.message}'));
} on HtHttpException catch (e) {
// Catch any other HtHttpException subtypes
emit(AuthenticationFailure('HTTP error: ${e.message}'));
final message = e.message.isNotEmpty ? e.message : 'An unspecified HTTP error occurred.';
emit(AuthenticationFailure('HTTP error: $message'));
} catch (e) {
// Catch any other unexpected errors
emit(AuthenticationFailure('An unexpected error occurred: $e'));
Expand All @@ -102,9 +104,9 @@ class AuthenticationBloc
// On success, the _AuthenticationUserChanged listener will handle
// emitting AuthenticationAuthenticated.
} on InvalidInputException catch (e) {
emit(AuthenticationFailure('Invalid input: ${e.message}'));
emit(AuthenticationFailure(e.message)); // Use specific error message
} on AuthenticationException catch (e) {
emit(AuthenticationFailure('Authentication failed: ${e.message}'));
emit(AuthenticationFailure(e.message)); // Use specific error message
} on NetworkException catch (_) {
emit(const AuthenticationFailure('Network error occurred.'));
} on ServerException catch (e) {
Expand Down Expand Up @@ -155,6 +157,10 @@ class AuthenticationBloc
await _authenticationRepository.signOut();
// On success, the _AuthenticationUserChanged listener will handle
// emitting AuthenticationUnauthenticated.
// No need to emit AuthenticationLoading() before calling signOut if
// the authStateChanges listener handles the subsequent state update.
// However, if immediate feedback is desired, it can be kept.
// For now, let's assume the listener is sufficient.
} on NetworkException catch (_) {
emit(const AuthenticationFailure('Network error occurred.'));
} on ServerException catch (e) {
Expand All @@ -168,4 +174,10 @@ class AuthenticationBloc
emit(AuthenticationFailure('An unexpected error occurred: $e'));
}
}

@override
Future<void> close() {
_userAuthSubscription.cancel();
return super.close();
}
}
5 changes: 4 additions & 1 deletion lib/authentication/view/email_code_verification_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ class _EmailCodeVerificationFormState
const SizedBox(height: AppSpacing.xxl), // Increased spacing
ElevatedButton(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: AppSpacing.md),
padding: const EdgeInsets.symmetric(
vertical: AppSpacing.md,
horizontal: AppSpacing.lg, // Added horizontal padding
),
textStyle: textTheme.labelLarge,
),
onPressed: widget.isLoading ? null : _submitForm,
Expand Down
Loading