Skip to content

Commit 671691f

Browse files
authored
Merge pull request #10 from headlines-toolkit/fix_rbac
refactor(auth): streamline user retrieval and add admin user reposito…
2 parents 1211eec + 534cadc commit 671691f

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

lib/src/services/auth_service.dart

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,7 @@ class AuthService {
6565
try {
6666
// For dashboard login, first validate the user exists and has permissions.
6767
if (isDashboardLogin) {
68-
print('Dashboard login initiated for $email. Verifying user...');
69-
User? user;
70-
try {
71-
final query = {'email': email};
72-
final response = await _userRepository.readAllByQuery(query);
73-
if (response.items.isNotEmpty) {
74-
user = response.items.first;
75-
}
76-
} on HtHttpException catch (e) {
77-
print('Repository error while verifying dashboard user $email: $e');
78-
rethrow;
79-
}
80-
68+
final user = await _findUserByEmail(email);
8169
if (user == null) {
8270
print('Dashboard login failed: User $email not found.');
8371
throw const UnauthorizedException(
@@ -162,12 +150,9 @@ class AuthService {
162150
User user;
163151
try {
164152
// Attempt to find user by email
165-
final query = {'email': email};
166-
final paginatedResponse = await _userRepository.readAllByQuery(query);
167-
168-
if (paginatedResponse.items.isNotEmpty) {
169-
user = paginatedResponse.items.first;
170-
print('Found existing user: ${user.id} for email $email');
153+
final existingUser = await _findUserByEmail(email);
154+
if (existingUser != null) {
155+
user = existingUser;
171156
} else {
172157
// User not found.
173158
if (isDashboardLogin) {
@@ -556,4 +541,21 @@ class AuthService {
556541
throw OperationFailedException('Failed to delete user account: $e');
557542
}
558543
}
544+
545+
/// Finds a user by their email address.
546+
///
547+
/// Returns the [User] if found, otherwise `null`.
548+
/// Re-throws any [HtHttpException] from the repository.
549+
Future<User?> _findUserByEmail(String email) async {
550+
try {
551+
final query = {'email': email};
552+
final response = await _userRepository.readAllByQuery(query);
553+
if (response.items.isNotEmpty) {
554+
return response.items.first;
555+
}
556+
return null;
557+
} on HtHttpException {
558+
rethrow;
559+
}
560+
}
559561
}

routes/_middleware.dart

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ HtDataRepository<Country> _createCountryRepository() {
111111
return HtDataRepository<Country>(dataClient: client);
112112
}
113113

114+
HtDataRepository<User> _createAdminUserRepository() {
115+
print('Initializing User Repository with Admin...');
116+
// This assumes `adminUserFixtureData` is available from `ht_shared`.
117+
final initialData = usersFixturesData;
118+
final client = HtDataInMemory<User>(
119+
toJson: (u) => u.toJson(),
120+
getId: (u) => u.id,
121+
initialData: initialData,
122+
);
123+
print('User Repository Initialized with admin user.');
124+
return HtDataRepository<User>(dataClient: client);
125+
}
126+
114127
// New repositories for user settings and preferences
115128
HtDataRepository<UserAppSettings> _createUserAppSettingsRepository() {
116129
print('Initializing UserAppSettings Repository...');
@@ -187,15 +200,9 @@ Handler middleware(Handler handler) {
187200
const uuid = Uuid();
188201

189202
// --- Auth Dependencies ---
190-
// User Repo (using InMemory for now)
191-
final userRepository = HtDataRepository<User>(
192-
dataClient: HtDataInMemory<User>(
193-
toJson: (u) => u.toJson(),
194-
getId: (u) => u.id,
195-
// No initial user data fixture needed for auth flow typically
196-
),
197-
);
198-
print('[MiddlewareSetup] HtDataRepository<User> instantiated.');
203+
// User Repo with pre-loaded admin user
204+
final userRepository = _createAdminUserRepository();
205+
print('[MiddlewareSetup] HtDataRepository<User> with admin user instantiated.');
199206
// Email Repo (using InMemory)
200207
const emailRepository = HtEmailRepository(
201208
emailClient: HtEmailInMemoryClient(),

0 commit comments

Comments
 (0)