Skip to content

Commit 80c1ed5

Browse files
committed
refactor(app): update AppBloc to handle user roles list
This refactors the AppBloc's user change handler to correctly evaluate dashboard access based on the user.roles list instead of a singular role property. The logic now checks for 'admin' or 'publisher' roles to grant authenticated status. It also corrects the condition for fetching user settings to depend on this authenticated status, not just the presence of a user object.
1 parent 32c33b8 commit 80c1ed5

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,40 @@ class AppBloc extends Bloc<AppEvent, AppState> {
4545
AppUserChanged event,
4646
Emitter<AppState> emit,
4747
) async {
48-
// Determine the AppStatus based on the user object and its role
48+
final user = event.user;
4949
final AppStatus status;
5050

51-
switch (event.user?.role) {
52-
case null:
53-
status = AppStatus.unauthenticated;
54-
case UserRole.standardUser:
55-
status = AppStatus.authenticated;
56-
// ignore: no_default_cases
57-
default: // Fallback for any other roles not explicitly handled
58-
status = AppStatus
59-
.unauthenticated; // Treat other roles as unauthenticated for dashboard
51+
if (user != null &&
52+
(user.roles.contains(UserRoles.admin) ||
53+
user.roles.contains(UserRoles.publisher))) {
54+
status = AppStatus.authenticated;
55+
} else {
56+
status = AppStatus.unauthenticated;
6057
}
6158

6259
// Emit user and status update
63-
emit(state.copyWith(status: status, user: event.user));
60+
emit(state.copyWith(status: status, user: user));
6461

6562
// If user is authenticated, load their app settings
66-
if (event.user != null) {
63+
if (status == AppStatus.authenticated && user != null) {
6764
try {
6865
final userAppSettings = await _userAppSettingsRepository.read(
69-
id: event.user!.id,
66+
id: user.id,
7067
);
7168
emit(state.copyWith(userAppSettings: userAppSettings));
7269
} on NotFoundException {
7370
// If settings not found, create default ones
74-
final defaultSettings = UserAppSettings(id: event.user!.id);
71+
final defaultSettings = UserAppSettings(id: user.id);
7572
await _userAppSettingsRepository.create(item: defaultSettings);
7673
emit(state.copyWith(userAppSettings: defaultSettings));
7774
} on HtHttpException catch (e) {
7875
// Handle HTTP exceptions during settings load
7976
print('Error loading user app settings: ${e.message}');
80-
emit(state.copyWith()); // Clear settings on error
77+
emit(state.copyWith(clearUserAppSettings: true));
8178
} catch (e) {
8279
// Handle any other unexpected errors
8380
print('Unexpected error loading user app settings: $e');
84-
emit(state.copyWith()); // Clear settings on error
81+
emit(state.copyWith(clearUserAppSettings: true));
8582
}
8683
} else {
8784
// If user is unauthenticated, clear app settings

0 commit comments

Comments
 (0)