diff --git a/lib/src/services/auth_service.dart b/lib/src/services/auth_service.dart index 3af933a..186ec8a 100644 --- a/lib/src/services/auth_service.dart +++ b/lib/src/services/auth_service.dart @@ -65,19 +65,7 @@ class AuthService { try { // For dashboard login, first validate the user exists and has permissions. if (isDashboardLogin) { - print('Dashboard login initiated for $email. Verifying user...'); - User? user; - try { - final query = {'email': email}; - final response = await _userRepository.readAllByQuery(query); - if (response.items.isNotEmpty) { - user = response.items.first; - } - } on HtHttpException catch (e) { - print('Repository error while verifying dashboard user $email: $e'); - rethrow; - } - + final user = await _findUserByEmail(email); if (user == null) { print('Dashboard login failed: User $email not found.'); throw const UnauthorizedException( @@ -162,12 +150,9 @@ class AuthService { User user; try { // Attempt to find user by email - final query = {'email': email}; - final paginatedResponse = await _userRepository.readAllByQuery(query); - - if (paginatedResponse.items.isNotEmpty) { - user = paginatedResponse.items.first; - print('Found existing user: ${user.id} for email $email'); + final existingUser = await _findUserByEmail(email); + if (existingUser != null) { + user = existingUser; } else { // User not found. if (isDashboardLogin) { @@ -556,4 +541,21 @@ class AuthService { throw OperationFailedException('Failed to delete user account: $e'); } } + + /// Finds a user by their email address. + /// + /// Returns the [User] if found, otherwise `null`. + /// Re-throws any [HtHttpException] from the repository. + Future _findUserByEmail(String email) async { + try { + final query = {'email': email}; + final response = await _userRepository.readAllByQuery(query); + if (response.items.isNotEmpty) { + return response.items.first; + } + return null; + } on HtHttpException { + rethrow; + } + } } diff --git a/routes/_middleware.dart b/routes/_middleware.dart index ca00c7b..15ae1ef 100644 --- a/routes/_middleware.dart +++ b/routes/_middleware.dart @@ -111,6 +111,19 @@ HtDataRepository _createCountryRepository() { return HtDataRepository(dataClient: client); } +HtDataRepository _createAdminUserRepository() { + print('Initializing User Repository with Admin...'); + // This assumes `adminUserFixtureData` is available from `ht_shared`. + final initialData = usersFixturesData; + final client = HtDataInMemory( + toJson: (u) => u.toJson(), + getId: (u) => u.id, + initialData: initialData, + ); + print('User Repository Initialized with admin user.'); + return HtDataRepository(dataClient: client); +} + // New repositories for user settings and preferences HtDataRepository _createUserAppSettingsRepository() { print('Initializing UserAppSettings Repository...'); @@ -187,15 +200,9 @@ Handler middleware(Handler handler) { const uuid = Uuid(); // --- Auth Dependencies --- - // User Repo (using InMemory for now) - final userRepository = HtDataRepository( - dataClient: HtDataInMemory( - toJson: (u) => u.toJson(), - getId: (u) => u.id, - // No initial user data fixture needed for auth flow typically - ), - ); - print('[MiddlewareSetup] HtDataRepository instantiated.'); + // User Repo with pre-loaded admin user + final userRepository = _createAdminUserRepository(); + print('[MiddlewareSetup] HtDataRepository with admin user instantiated.'); // Email Repo (using InMemory) const emailRepository = HtEmailRepository( emailClient: HtEmailInMemoryClient(),