@@ -65,19 +65,7 @@ class AuthService {
65
65
try {
66
66
// For dashboard login, first validate the user exists and has permissions.
67
67
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);
81
69
if (user == null ) {
82
70
print ('Dashboard login failed: User $email not found.' );
83
71
throw const UnauthorizedException (
@@ -162,12 +150,9 @@ class AuthService {
162
150
User user;
163
151
try {
164
152
// 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;
171
156
} else {
172
157
// User not found.
173
158
if (isDashboardLogin) {
@@ -556,4 +541,21 @@ class AuthService {
556
541
throw OperationFailedException ('Failed to delete user account: $e ' );
557
542
}
558
543
}
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
+ }
559
561
}
0 commit comments