Skip to content

Commit b915b5d

Browse files
committed
refactor(auth): replace print statements with logging in SimpleAuthTokenService
- Inject Logger into SimpleAuthTokenService - Replace print statements with appropriate log levels - Update log messages to use logging conventions - Remove unnecessary print statements and use logging for all output
1 parent d42156e commit b915b5d

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

lib/src/services/simple_auth_token_service.dart

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:ht_api/src/services/auth_token_service.dart';
22
import 'package:ht_data_repository/ht_data_repository.dart';
33
import 'package:ht_shared/ht_shared.dart';
4+
import 'package:logging/logging.dart';
45

56
/// {@template simple_auth_token_service}
67
/// A minimal, dependency-free implementation of [AuthTokenService] for debugging.
@@ -10,63 +11,61 @@ import 'package:ht_shared/ht_shared.dart';
1011
/// {@endtemplate}
1112
class SimpleAuthTokenService implements AuthTokenService {
1213
/// {@macro simple_auth_token_service}
13-
const SimpleAuthTokenService({required HtDataRepository<User> userRepository})
14-
: _userRepository = userRepository;
14+
const SimpleAuthTokenService({
15+
required HtDataRepository<User> userRepository,
16+
required Logger log,
17+
}) : _userRepository = userRepository,
18+
_log = log;
1519

1620
final HtDataRepository<User> _userRepository;
21+
final Logger _log;
1722
static const String _tokenPrefix = 'valid-token-for-user-id:';
1823

1924
@override
2025
Future<String> generateToken(User user) async {
21-
print('[SimpleAuthTokenService] Generating token for user ${user.id}');
26+
_log.info('Generating token for user ${user.id}');
2227
final token = '$_tokenPrefix${user.id}';
23-
print('[SimpleAuthTokenService] Generated token: $token');
28+
_log.finer('Generated token: $token');
2429
// Simulate async operation if needed, though not strictly necessary here
2530
await Future<void>.delayed(Duration.zero);
2631
return token;
2732
}
2833

2934
@override
3035
Future<User?> validateToken(String token) async {
31-
print('[SimpleAuthTokenService] Attempting to validate token: $token');
36+
_log.finer('Attempting to validate token: $token');
3237
if (!token.startsWith(_tokenPrefix)) {
33-
print('[SimpleAuthTokenService] Validation failed: Invalid prefix.');
38+
_log.warning('Validation failed: Invalid prefix.');
3439
// Mimic JWT behavior by throwing Unauthorized for invalid format
3540
throw const UnauthorizedException('Invalid token format.');
3641
}
3742

3843
final userId = token.substring(_tokenPrefix.length);
39-
print('[SimpleAuthTokenService] Extracted user ID: $userId');
44+
_log.finer('Extracted user ID: $userId');
4045

4146
if (userId.isEmpty) {
42-
print('[SimpleAuthTokenService] Validation failed: Empty user ID.');
47+
_log.warning('Validation failed: Empty user ID.');
4348
throw const UnauthorizedException('Invalid token: Empty user ID.');
4449
}
4550

4651
try {
47-
print(
48-
'[SimpleAuthTokenService] Attempting to read user from repository...',
49-
);
52+
_log.finer('Attempting to read user from repository...');
5053
final user = await _userRepository.read(id: userId);
51-
print('[SimpleAuthTokenService] User read successful: ${user.id}');
54+
_log.info('User read successful: ${user.id}');
5255
return user;
5356
} on NotFoundException {
54-
print(
55-
'[SimpleAuthTokenService] Validation failed: User ID $userId not found.',
56-
);
57+
_log.warning('Validation failed: User ID $userId not found.');
5758
// Return null if user not found, mimicking successful validation
5859
// of a token for a non-existent user. The middleware handles this.
5960
return null;
6061
} on HtHttpException catch (e, s) {
6162
// Handle other potential repository errors
62-
print(
63-
'[SimpleAuthTokenService] Validation failed: Repository error $e\n$s',
64-
);
63+
_log.warning('Validation failed: Repository error', e, s);
6564
// Re-throw other client/repo exceptions
6665
rethrow;
6766
} catch (e, s) {
6867
// Catch unexpected errors during validation
69-
print('[SimpleAuthTokenService] Unexpected validation error: $e\n$s');
68+
_log.severe('Unexpected validation error', e, s);
7069
throw OperationFailedException(
7170
'Simple token validation failed unexpectedly: $e',
7271
);
@@ -78,8 +77,8 @@ class SimpleAuthTokenService implements AuthTokenService {
7877
// This service uses simple prefixed tokens, not JWTs with JTI.
7978
// True invalidation/blacklisting isn't applicable here.
8079
// This method is implemented to satisfy the AuthTokenService interface.
81-
print(
82-
'[SimpleAuthTokenService] Received request to invalidate token: $token. '
80+
_log.info(
81+
'Received request to invalidate token: $token. '
8382
'No server-side invalidation is performed for simple tokens.',
8483
);
8584
// Simulate async operation

0 commit comments

Comments
 (0)