Skip to content

Commit d221b6c

Browse files
authored
Merge pull request #23 from headlines-toolkit/refactor_sync_the_auth_feature_with_the_backend_and_client_update
Refactor sync the auth feature with the backend and client update
2 parents 1c56895 + 91ca0b3 commit d221b6c

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
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

lib/app_configuration/view/app_configuration_page.dart

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
283283
),
284284
children: [
285285
_UserPreferenceLimitsForm(
286-
userRole: UserRole.guestUser,
286+
userRole: UserRoles.guestUser,
287287
appConfig: appConfig,
288288
onConfigChanged: (newConfig) {
289289
context.read<AppConfigurationBloc>().add(
@@ -303,7 +303,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
303303
),
304304
children: [
305305
_UserPreferenceLimitsForm(
306-
userRole: UserRole.standardUser,
306+
userRole: UserRoles.standardUser,
307307
appConfig: appConfig,
308308
onConfigChanged: (newConfig) {
309309
context.read<AppConfigurationBloc>().add(
@@ -323,7 +323,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
323323
),
324324
children: [
325325
_UserPreferenceLimitsForm(
326-
userRole: UserRole.premiumUser,
326+
userRole: UserRoles.premiumUser,
327327
appConfig: appConfig,
328328
onConfigChanged: (newConfig) {
329329
context.read<AppConfigurationBloc>().add(
@@ -359,7 +359,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
359359
),
360360
children: [
361361
_AdConfigForm(
362-
userRole: UserRole.guestUser,
362+
userRole: UserRoles.guestUser,
363363
appConfig: appConfig,
364364
onConfigChanged: (newConfig) {
365365
context.read<AppConfigurationBloc>().add(
@@ -379,7 +379,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
379379
),
380380
children: [
381381
_AdConfigForm(
382-
userRole: UserRole.standardUser,
382+
userRole: UserRoles.standardUser,
383383
appConfig: appConfig,
384384
onConfigChanged: (newConfig) {
385385
context.read<AppConfigurationBloc>().add(
@@ -399,7 +399,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
399399
),
400400
children: [
401401
_AdConfigForm(
402-
userRole: UserRole.premiumUser,
402+
userRole: UserRoles.premiumUser,
403403
appConfig: appConfig,
404404
onConfigChanged: (newConfig) {
405405
context.read<AppConfigurationBloc>().add(
@@ -438,7 +438,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
438438
),
439439
children: [
440440
_AccountActionConfigForm(
441-
userRole: UserRole.guestUser,
441+
userRole: UserRoles.guestUser,
442442
appConfig: appConfig,
443443
onConfigChanged: (newConfig) {
444444
context.read<AppConfigurationBloc>().add(
@@ -458,7 +458,7 @@ class _AppConfigurationPageState extends State<AppConfigurationPage> {
458458
),
459459
children: [
460460
_AccountActionConfigForm(
461-
userRole: UserRole.standardUser,
461+
userRole: UserRoles.standardUser,
462462
appConfig: appConfig,
463463
onConfigChanged: (newConfig) {
464464
context.read<AppConfigurationBloc>().add(
@@ -779,7 +779,7 @@ class _UserPreferenceLimitsForm extends StatefulWidget {
779779
required this.buildIntField,
780780
});
781781

782-
final UserRole userRole;
782+
final String userRole;
783783
final AppConfig appConfig;
784784
final ValueChanged<AppConfig> onConfigChanged;
785785
final Widget Function(
@@ -936,7 +936,7 @@ class _UserPreferenceLimitsFormState extends State<_UserPreferenceLimitsForm> {
936936
final userPreferenceLimits = widget.appConfig.userPreferenceLimits;
937937

938938
switch (widget.userRole) {
939-
case UserRole.guestUser:
939+
case UserRoles.guestUser:
940940
return Column(
941941
children: [
942942
widget.buildIntField(
@@ -975,7 +975,7 @@ class _UserPreferenceLimitsFormState extends State<_UserPreferenceLimitsForm> {
975975
),
976976
],
977977
);
978-
case UserRole.standardUser:
978+
case UserRoles.standardUser:
979979
return Column(
980980
children: [
981981
widget.buildIntField(
@@ -1015,7 +1015,7 @@ class _UserPreferenceLimitsFormState extends State<_UserPreferenceLimitsForm> {
10151015
),
10161016
],
10171017
);
1018-
case UserRole.premiumUser:
1018+
case UserRoles.premiumUser:
10191019
return Column(
10201020
children: [
10211021
widget.buildIntField(
@@ -1055,10 +1055,12 @@ class _UserPreferenceLimitsFormState extends State<_UserPreferenceLimitsForm> {
10551055
),
10561056
],
10571057
);
1058-
case UserRole.admin:
1058+
case UserRoles.admin:
10591059
// Admin role might not have specific limits here, or could be
10601060
// a separate configuration. For now, return empty.
10611061
return const SizedBox.shrink();
1062+
default:
1063+
return const SizedBox.shrink();
10621064
}
10631065
}
10641066
}
@@ -1071,7 +1073,7 @@ class _AdConfigForm extends StatefulWidget {
10711073
required this.buildIntField,
10721074
});
10731075

1074-
final UserRole userRole;
1076+
final String userRole;
10751077
final AppConfig appConfig;
10761078
final ValueChanged<AppConfig> onConfigChanged;
10771079
final Widget Function(
@@ -1271,7 +1273,7 @@ class _AdConfigFormState extends State<_AdConfigForm> {
12711273
final adConfig = widget.appConfig.adConfig;
12721274

12731275
switch (widget.userRole) {
1274-
case UserRole.guestUser:
1276+
case UserRoles.guestUser:
12751277
return Column(
12761278
children: [
12771279
widget.buildIntField(
@@ -1329,7 +1331,7 @@ class _AdConfigFormState extends State<_AdConfigForm> {
13291331
),
13301332
],
13311333
);
1332-
case UserRole.standardUser:
1334+
case UserRoles.standardUser:
13331335
return Column(
13341336
children: [
13351337
widget.buildIntField(
@@ -1391,7 +1393,7 @@ class _AdConfigFormState extends State<_AdConfigForm> {
13911393
),
13921394
],
13931395
);
1394-
case UserRole.premiumUser:
1396+
case UserRoles.premiumUser:
13951397
return Column(
13961398
children: [
13971399
widget.buildIntField(
@@ -1450,7 +1452,9 @@ class _AdConfigFormState extends State<_AdConfigForm> {
14501452
),
14511453
],
14521454
);
1453-
case UserRole.admin:
1455+
case UserRoles.admin:
1456+
return const SizedBox.shrink();
1457+
default:
14541458
return const SizedBox.shrink();
14551459
}
14561460
}
@@ -1464,7 +1468,7 @@ class _AccountActionConfigForm extends StatefulWidget {
14641468
required this.buildIntField,
14651469
});
14661470

1467-
final UserRole userRole;
1471+
final String userRole;
14681472
final AppConfig appConfig;
14691473
final ValueChanged<AppConfig> onConfigChanged;
14701474
final Widget Function(
@@ -1553,7 +1557,7 @@ class _AccountActionConfigFormState extends State<_AccountActionConfigForm> {
15531557
final accountActionConfig = widget.appConfig.accountActionConfig;
15541558

15551559
switch (widget.userRole) {
1556-
case UserRole.guestUser:
1560+
case UserRoles.guestUser:
15571561
return Column(
15581562
children: [
15591563
widget.buildIntField(
@@ -1576,7 +1580,7 @@ class _AccountActionConfigFormState extends State<_AccountActionConfigForm> {
15761580
),
15771581
],
15781582
);
1579-
case UserRole.standardUser:
1583+
case UserRoles.standardUser:
15801584
return Column(
15811585
children: [
15821586
widget.buildIntField(
@@ -1599,8 +1603,10 @@ class _AccountActionConfigFormState extends State<_AccountActionConfigForm> {
15991603
),
16001604
],
16011605
);
1602-
case UserRole.premiumUser:
1603-
case UserRole.admin:
1606+
case UserRoles.premiumUser:
1607+
case UserRoles.admin:
1608+
return const SizedBox.shrink();
1609+
default:
16041610
return const SizedBox.shrink();
16051611
}
16061612
}

lib/authentication/bloc/authentication_bloc.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import 'package:ht_auth_repository/ht_auth_repository.dart';
66
import 'package:ht_shared/ht_shared.dart'
77
show
88
AuthenticationException,
9+
ForbiddenException,
910
HtHttpException,
1011
InvalidInputException,
1112
NetworkException,
13+
NotFoundException,
1214
OperationFailedException,
1315
ServerException,
16+
UnauthorizedException,
1417
User;
1518

1619
part 'authentication_event.dart';
@@ -65,10 +68,17 @@ class AuthenticationBloc
6568
}
6669
emit(AuthenticationRequestCodeLoading());
6770
try {
68-
await _authenticationRepository.requestSignInCode(event.email);
71+
await _authenticationRepository.requestSignInCode(
72+
event.email,
73+
isDashboardLogin: true,
74+
);
6975
emit(AuthenticationCodeSentSuccess(email: event.email));
7076
} on InvalidInputException catch (e) {
7177
emit(AuthenticationFailure('Invalid input: ${e.message}'));
78+
} on UnauthorizedException catch (e) {
79+
emit(AuthenticationFailure(e.message));
80+
} on ForbiddenException catch (e) {
81+
emit(AuthenticationFailure(e.message));
7282
} on NetworkException catch (_) {
7383
emit(const AuthenticationFailure('Network error occurred.'));
7484
} on ServerException catch (e) {
@@ -95,13 +105,19 @@ class AuthenticationBloc
95105
) async {
96106
emit(AuthenticationLoading());
97107
try {
98-
await _authenticationRepository.verifySignInCode(event.email, event.code);
108+
await _authenticationRepository.verifySignInCode(
109+
event.email,
110+
event.code,
111+
isDashboardLogin: true,
112+
);
99113
// On success, the _AuthenticationUserChanged listener will handle
100114
// emitting AuthenticationAuthenticated.
101115
} on InvalidInputException catch (e) {
102116
emit(AuthenticationFailure(e.message));
103117
} on AuthenticationException catch (e) {
104118
emit(AuthenticationFailure(e.message));
119+
} on NotFoundException catch (e) {
120+
emit(AuthenticationFailure(e.message));
105121
} on NetworkException catch (_) {
106122
emit(const AuthenticationFailure('Network error occurred.'));
107123
} on ServerException catch (e) {

pubspec.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ packages:
205205
description:
206206
path: "."
207207
ref: HEAD
208-
resolved-ref: fd31ce8e255c27e1fcc17e849c41f9c8511a6d87
208+
resolved-ref: f89241dfd482d2a72b1168f979597a34b1004df5
209209
url: "https://github.com/headlines-toolkit/ht-auth-api.git"
210210
source: git
211211
version: "0.0.0"
@@ -214,7 +214,7 @@ packages:
214214
description:
215215
path: "."
216216
ref: HEAD
217-
resolved-ref: f9c3b44b79fc19dfd9b9a7e0d1e21e60f4885617
217+
resolved-ref: a003eb493db4fc134db419a721ee2fda0b598032
218218
url: "https://github.com/headlines-toolkit/ht-auth-client.git"
219219
source: git
220220
version: "0.0.0"
@@ -223,7 +223,7 @@ packages:
223223
description:
224224
path: "."
225225
ref: HEAD
226-
resolved-ref: "3a8dc5ff81c59805fa59996517eb0fdf136a0b67"
226+
resolved-ref: "721a028b926a5a8af2b5176de039cd6394a21724"
227227
url: "https://github.com/headlines-toolkit/ht-auth-inmemory"
228228
source: git
229229
version: "0.0.0"
@@ -232,7 +232,7 @@ packages:
232232
description:
233233
path: "."
234234
ref: HEAD
235-
resolved-ref: "596ba311cdbbdf61a216f60dac0218fab9e234d9"
235+
resolved-ref: b7de5cc86d432b17710c83a1bf8de105bb4fa00d
236236
url: "https://github.com/headlines-toolkit/ht-auth-repository.git"
237237
source: git
238238
version: "0.0.0"
@@ -286,7 +286,7 @@ packages:
286286
description:
287287
path: "."
288288
ref: HEAD
289-
resolved-ref: e2860560d21c1cf43f0e65f28c9ba722823254f2
289+
resolved-ref: "2378d6698df1cdeb7c5a17470f94fb8a5a99ca01"
290290
url: "https://github.com/headlines-toolkit/ht-kv-storage-service.git"
291291
source: git
292292
version: "0.0.0"
@@ -304,7 +304,7 @@ packages:
304304
description:
305305
path: "."
306306
ref: HEAD
307-
resolved-ref: b3a339a2957b35a2bb7baf249e89ef9ca296eb3e
307+
resolved-ref: "30aff4d0e2661ff79f2b84070af5f7982d88ba66"
308308
url: "https://github.com/headlines-toolkit/ht-shared.git"
309309
source: git
310310
version: "0.0.0"

0 commit comments

Comments
 (0)