@@ -82,46 +82,83 @@ class JwtAuthTokenService implements AuthTokenService {
82
82
83
83
@override
84
84
Future <User ?> validateToken (String token) async {
85
+ print ('[validateToken] Attempting to validate token...' );
85
86
try {
86
87
// Verify the token's signature and expiry
88
+ print ('[validateToken] Verifying token signature and expiry...' );
87
89
final jwt = JWT .verify (token, SecretKey (_secretKey));
90
+ print ('[validateToken] Token verified. Payload: ${jwt .payload }' );
88
91
89
92
// Extract user ID from the subject claim
90
- final userId = jwt.payload['sub' ] as String ? ;
91
- if (userId == null ) {
92
- print ('Token validation failed: Missing "sub" claim.' );
93
+ final subClaim = jwt.payload['sub' ];
94
+ print (
95
+ '[validateToken] Extracted "sub" claim: $subClaim '
96
+ '(Type: ${subClaim .runtimeType })' ,
97
+ );
98
+
99
+ // Safely attempt to cast to String
100
+ String ? userId;
101
+ if (subClaim is String ) {
102
+ userId = subClaim;
103
+ print ('[validateToken] "sub" claim successfully cast to String: $userId ' );
104
+ } else if (subClaim != null ) {
105
+ print (
106
+ '[validateToken] WARNING: "sub" claim is not a String. '
107
+ 'Attempting toString().' ,
108
+ );
109
+ // Handle potential non-string types if necessary, or throw error
110
+ // For now, let's treat non-string sub as an error
111
+ throw BadRequestException (
112
+ 'Malformed token: "sub" claim is not a String '
113
+ '(Type: ${subClaim .runtimeType }).' ,
114
+ );
115
+ }
116
+
117
+ if (userId == null || userId.isEmpty) {
118
+ print ('[validateToken] Token validation failed: Missing or empty "sub" claim.' );
93
119
// Throw specific exception for malformed token
94
120
throw const BadRequestException (
95
- 'Malformed token: Missing subject claim.' ,
121
+ 'Malformed token: Missing or empty subject claim.' ,
96
122
);
97
123
}
98
124
125
+ print ('[validateToken] Attempting to fetch user with ID: $userId ' );
99
126
// Fetch the full user object from the repository
100
127
// This ensures the user still exists and is valid
101
128
final user = await _userRepository.read (userId);
102
- print ('Token validated successfully for user ${user .id }' );
129
+ print ('[validateToken] User repository read successful for ID: $userId ' );
130
+ print ('[validateToken] Token validated successfully for user ${user .id }' );
103
131
return user;
104
- } on JWTExpiredException {
105
- print ('Token validation failed : Token expired.' );
132
+ } on JWTExpiredException catch (e, s) {
133
+ print ('[validateToken] CATCH JWTExpiredException : Token expired. $ e \n $ s ' );
106
134
// Throw specific exception for expired token
107
135
throw const UnauthorizedException ('Token expired.' );
108
- } on JWTInvalidException catch (e) {
109
- print ('Token validation failed: Invalid token. Reason: ${e .message }' );
136
+ } on JWTInvalidException catch (e, s) {
137
+ print (
138
+ '[validateToken] CATCH JWTInvalidException: Invalid token. '
139
+ 'Reason: ${e .message }\n $s ' ,
140
+ );
110
141
// Throw specific exception for invalid token signature/format
111
142
throw UnauthorizedException ('Invalid token: ${e .message }' );
112
- } on JWTException catch (e) {
143
+ } on JWTException catch (e, s ) {
113
144
// Use JWTException as the general catch-all
114
- print ('Token validation failed: JWT Exception. Reason: ${e .message }' );
145
+ print (
146
+ '[validateToken] CATCH JWTException: General JWT error. '
147
+ 'Reason: ${e .message }\n $s ' ,
148
+ );
115
149
// Treat other JWT exceptions as invalid tokens
116
150
throw UnauthorizedException ('Invalid token: ${e .message }' );
117
- } on HtHttpException catch (e) {
151
+ } on HtHttpException catch (e, s ) {
118
152
// Handle errors from the user repository (e.g., user not found)
119
- print ('Token validation failed: Error fetching user $e ' );
153
+ print (
154
+ '[validateToken] CATCH HtHttpException: Error fetching user. '
155
+ 'Type: ${e .runtimeType }, Message: $e \n $s ' ,
156
+ );
120
157
// Re-throw repository exceptions directly for the error handler
121
158
rethrow ;
122
- } catch (e) {
159
+ } catch (e, s ) {
123
160
// Catch unexpected errors during validation
124
- print ('Unexpected error during token validation : $e ' );
161
+ print ('[validateToken] CATCH UNEXPECTED Exception : $e \n $ s ' );
125
162
// Wrap unexpected errors in a standard exception type
126
163
throw OperationFailedException (
127
164
'Token validation failed unexpectedly: $e ' ,
0 commit comments