@@ -11,6 +11,9 @@ function getCurrentTokenInfo() {
1111 const files = fs . readdirSync ( tempDir ) ;
1212 const tokenFiles = files . filter ( file => file . startsWith ( 'sigma-portal-token-' ) && file . endsWith ( '.json' ) ) ;
1313
14+ console . log ( '=== EXECUTE ROUTE TOKEN DEBUGGING ===' ) ;
15+ console . log ( 'Found token files:' , tokenFiles ) ;
16+
1417 let namedConfigTokens = [ ] ;
1518 let defaultToken = null ;
1619
@@ -23,6 +26,7 @@ function getCurrentTokenInfo() {
2326 // Check if token is still valid (not expired)
2427 if ( tokenData . expiresAt && now < tokenData . expiresAt ) {
2528 const lastAccessTime = tokenData . lastAccessed || tokenData . createdAt ;
29+ console . log ( `Token ${ file } : clientId=${ tokenData . clientId ?. substring ( 0 , 8 ) || 'default' } , fullLength=${ tokenData . clientId ?. length || 0 } , createdAt=${ new Date ( tokenData . createdAt ) } , lastAccessed=${ tokenData . lastAccessed ? new Date ( tokenData . lastAccessed ) : 'none' } , lastAccessTime=${ lastAccessTime } ` ) ;
2630
2731 const tokenInfo = {
2832 hasValidToken : true ,
@@ -37,28 +41,43 @@ function getCurrentTokenInfo() {
3741 // Use same classification logic as /api/token
3842 if ( tokenData . clientId && tokenData . clientId . length > 8 ) {
3943 namedConfigTokens . push ( tokenInfo ) ;
44+ console . log ( ` -> This is a named config token (full clientId: ${ tokenData . clientId . length } chars)` ) ;
4045 } else {
4146 defaultToken = tokenInfo ;
47+ console . log ( ` -> This is the default token (clientId: ${ tokenData . clientId || 'none' } )` ) ;
4248 }
4349 } else {
50+ console . log ( `Token ${ file } is expired, removing...` ) ;
4451 // Token expired, remove file
4552 fs . unlinkSync ( filePath ) ;
4653 }
4754 } catch ( err ) {
55+ console . warn ( `Failed to read token file ${ file } :` , err ) ;
4856 // Skip invalid token files
4957 }
5058 }
5159
60+ console . log ( `Found ${ namedConfigTokens . length } named config tokens and ${ defaultToken ? 1 : 0 } default tokens` ) ;
61+
5262 // Prioritize named config tokens over default token (same as /api/token)
5363 if ( namedConfigTokens . length > 0 ) {
64+ // Sort named config tokens by most recent access time
5465 namedConfigTokens . sort ( ( a , b ) => b . lastAccessTime - a . lastAccessTime ) ;
66+ console . log ( 'Named config tokens sorted by lastAccessTime:' ) ;
67+ namedConfigTokens . forEach ( ( token , index ) => {
68+ console . log ( ` ${ index + 1 } . ${ token . clientId ?. substring ( 0 , 8 ) } ... - ${ new Date ( token . lastAccessTime ) } ` ) ;
69+ } ) ;
70+ console . log ( ` -> Selected most recent named config token: ${ namedConfigTokens [ 0 ] . clientId ?. substring ( 0 , 8 ) } ` ) ;
5571 return namedConfigTokens [ 0 ] ;
5672 } else if ( defaultToken ) {
73+ console . log ( ` -> No named config tokens, using default token` ) ;
5774 return defaultToken ;
5875 }
5976
77+ console . log ( ' -> No valid tokens found' ) ;
6078 return null ;
6179 } catch ( error ) {
80+ console . error ( 'Error in getCurrentTokenInfo:' , error ) ;
6281 return null ;
6382 }
6483}
0 commit comments