@@ -82,46 +82,83 @@ class JwtAuthTokenService implements AuthTokenService {
8282
8383 @override
8484 Future <User ?> validateToken (String token) async {
85+ print ('[validateToken] Attempting to validate token...' );
8586 try {
8687 // Verify the token's signature and expiry
88+ print ('[validateToken] Verifying token signature and expiry...' );
8789 final jwt = JWT .verify (token, SecretKey (_secretKey));
90+ print ('[validateToken] Token verified. Payload: ${jwt .payload }' );
8891
8992 // 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.' );
93119 // Throw specific exception for malformed token
94120 throw const BadRequestException (
95- 'Malformed token: Missing subject claim.' ,
121+ 'Malformed token: Missing or empty subject claim.' ,
96122 );
97123 }
98124
125+ print ('[validateToken] Attempting to fetch user with ID: $userId ' );
99126 // Fetch the full user object from the repository
100127 // This ensures the user still exists and is valid
101128 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 }' );
103131 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 ' );
106134 // Throw specific exception for expired token
107135 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+ );
110141 // Throw specific exception for invalid token signature/format
111142 throw UnauthorizedException ('Invalid token: ${e .message }' );
112- } on JWTException catch (e) {
143+ } on JWTException catch (e, s ) {
113144 // 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+ );
115149 // Treat other JWT exceptions as invalid tokens
116150 throw UnauthorizedException ('Invalid token: ${e .message }' );
117- } on HtHttpException catch (e) {
151+ } on HtHttpException catch (e, s ) {
118152 // 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+ );
120157 // Re-throw repository exceptions directly for the error handler
121158 rethrow ;
122- } catch (e) {
159+ } catch (e, s ) {
123160 // Catch unexpected errors during validation
124- print ('Unexpected error during token validation : $e ' );
161+ print ('[validateToken] CATCH UNEXPECTED Exception : $e \n $ s ' );
125162 // Wrap unexpected errors in a standard exception type
126163 throw OperationFailedException (
127164 'Token validation failed unexpectedly: $e ' ,
0 commit comments