@@ -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}
0 commit comments