@@ -487,12 +487,27 @@ export class ApiKeyManager extends BaseComponent {
487487 const { ApiClient } = await import ( '../api-client.js' ) ;
488488 const baseUrl = ApiClient . baseUrl || '/api/1' ;
489489
490+ // Get a valid token, avoid sending 'null'
491+ const authToken = this . _getApiKey ( ) ;
492+
493+ // Debug the token we're about to use
494+ console . log ( 'API Key Manager: Authorization token length:' , authToken ?. length || 0 ) ;
495+
496+ // Only include Authorization header if we have a valid token
497+ const headers = {
498+ 'Accept' : 'application/json'
499+ } ;
500+
501+ if ( authToken && authToken !== 'null' && authToken . length > 10 ) {
502+ console . log ( 'API Key Manager: Using valid token for Authorization header' ) ;
503+ headers [ 'Authorization' ] = `Bearer ${ authToken } ` ;
504+ } else {
505+ console . warn ( 'API Key Manager: No valid token available, proceeding without Authorization header' ) ;
506+ }
507+
490508 const response = await fetch ( `${ baseUrl } /api-keys` , {
491509 method : 'GET' ,
492- headers : {
493- 'Accept' : 'application/json' ,
494- 'Authorization' : `Bearer ${ this . _getApiKey ( ) } `
495- }
510+ headers
496511 } ) ;
497512
498513 if ( ! response . ok ) {
@@ -532,16 +547,28 @@ export class ApiKeyManager extends BaseComponent {
532547 const { ApiClient } = await import ( '../api-client.js' ) ;
533548 const baseUrl = ApiClient . baseUrl || '/api/1' ;
534549
550+ // Get a valid token, avoid sending 'null'
551+ const authToken = this . _getApiKey ( ) ;
552+
553+ // Debug the token we're about to use
554+ console . log ( 'API Key Manager: Create key - token length:' , authToken ?. length || 0 ) ;
555+
556+ // Only include Authorization header if we have a valid token
557+ const headers = {
558+ 'Content-Type' : 'application/json'
559+ } ;
560+
561+ if ( authToken && authToken !== 'null' && authToken . length > 10 ) {
562+ console . log ( 'API Key Manager: Create key - using valid token' ) ;
563+ headers [ 'Authorization' ] = `Bearer ${ authToken } ` ;
564+ } else {
565+ console . warn ( 'API Key Manager: Create key - no valid token available' ) ;
566+ }
567+
535568 const response = await fetch ( `${ baseUrl } /api-keys` , {
536569 method : 'POST' ,
537- headers : {
538- 'Content-Type' : 'application/json' ,
539- 'Accept' : 'application/json' ,
540- 'Authorization' : `Bearer ${ this . _getApiKey ( ) } `
541- } ,
542- body : JSON . stringify ( {
543- name : this . _newKeyName
544- } )
570+ headers,
571+ body : JSON . stringify ( { name : this . _newKeyName } )
545572 } ) ;
546573
547574 if ( ! response . ok ) {
@@ -576,13 +603,28 @@ export class ApiKeyManager extends BaseComponent {
576603 const { ApiClient } = await import ( '../api-client.js' ) ;
577604 const baseUrl = ApiClient . baseUrl || '/api/1' ;
578605
606+ // Get a valid token, avoid sending 'null'
607+ const authToken = this . _getApiKey ( ) ;
608+
609+ // Debug the token we're about to use
610+ console . log ( 'API Key Manager: Toggle key - token length:' , authToken ?. length || 0 ) ;
611+
612+ // Only include Authorization header if we have a valid token
613+ const headers = {
614+ 'Content-Type' : 'application/json' ,
615+ 'Accept' : 'application/json'
616+ } ;
617+
618+ if ( authToken && authToken !== 'null' && authToken . length > 10 ) {
619+ console . log ( 'API Key Manager: Toggle key - using valid token' ) ;
620+ headers [ 'Authorization' ] = `Bearer ${ authToken } ` ;
621+ } else {
622+ console . warn ( 'API Key Manager: Toggle key - no valid token available' ) ;
623+ }
624+
579625 const response = await fetch ( `${ baseUrl } /api-keys/${ keyId } ` , {
580626 method : 'PUT' ,
581- headers : {
582- 'Content-Type' : 'application/json' ,
583- 'Accept' : 'application/json' ,
584- 'Authorization' : `Bearer ${ this . _getApiKey ( ) } `
585- } ,
627+ headers,
586628 body : JSON . stringify ( {
587629 is_active : isActive
588630 } )
@@ -617,12 +659,27 @@ export class ApiKeyManager extends BaseComponent {
617659 const { ApiClient } = await import ( '../api-client.js' ) ;
618660 const baseUrl = ApiClient . baseUrl || '/api/1' ;
619661
662+ // Get a valid token, avoid sending 'null'
663+ const authToken = this . _getApiKey ( ) ;
664+
665+ // Debug the token we're about to use
666+ console . log ( 'API Key Manager: Delete key - token length:' , authToken ?. length || 0 ) ;
667+
668+ // Only include Authorization header if we have a valid token
669+ const headers = {
670+ 'Accept' : 'application/json'
671+ } ;
672+
673+ if ( authToken && authToken !== 'null' && authToken . length > 10 ) {
674+ console . log ( 'API Key Manager: Delete key - using valid token' ) ;
675+ headers [ 'Authorization' ] = `Bearer ${ authToken } ` ;
676+ } else {
677+ console . warn ( 'API Key Manager: Delete key - no valid token available' ) ;
678+ }
679+
620680 const response = await fetch ( `${ baseUrl } /api-keys/${ keyId } ` , {
621681 method : 'DELETE' ,
622- headers : {
623- 'Accept' : 'application/json' ,
624- 'Authorization' : `Bearer ${ this . _getApiKey ( ) } `
625- }
682+ headers
626683 } ) ;
627684
628685 if ( ! response . ok ) {
@@ -670,15 +727,30 @@ export class ApiKeyManager extends BaseComponent {
670727 * @private
671728 */
672729 _getApiKey ( ) {
673- // Try to get from localStorage
730+ // First, try to get JWT token
731+ const jwtToken = localStorage . getItem ( 'jwt_token' ) ;
732+ if ( jwtToken ) {
733+ console . log ( 'Using JWT token for authentication, length:' , jwtToken . length ) ;
734+ return jwtToken ;
735+ }
736+
737+ // Next, try API key from localStorage
674738 const apiKey = localStorage . getItem ( 'api_key' ) ;
675739 if ( apiKey ) {
740+ console . log ( 'Using API key from localStorage for authentication' ) ;
676741 return apiKey ;
677742 }
678743
679- // Try to get from URL
744+ // Finally, try to get from URL
680745 const urlParams = new URLSearchParams ( window . location . search ) ;
681- return urlParams . get ( 'api_key' ) ;
746+ const urlApiKey = urlParams . get ( 'api_key' ) ;
747+ if ( urlApiKey ) {
748+ console . log ( 'Using API key from URL for authentication' ) ;
749+ return urlApiKey ;
750+ }
751+
752+ console . warn ( 'No authentication token found in storage or URL' ) ;
753+ return null ;
682754 }
683755}
684756
0 commit comments