@@ -6,6 +6,7 @@ import { Subgraph } from './Subgraph';
66import { NLPParsingModal } from './NLPParsingModal' ;
77import type { Node , Edge , AttributeType , Morph } from './types' ;
88import { API_BASE_URL } from './api-config' ;
9+ import { keycloakAuth } from './services/keycloakAuth' ;
910import './NodeCard.css' ;
1011
1112interface NodeCardProps {
@@ -34,23 +35,29 @@ export function NodeCard({ node, allNodes, allRelations, attributes, isActive, o
3435 // Morph change state
3536 const [ isChangingMorph , setIsChangingMorph ] = useState ( false ) ;
3637
37- // Helper function for authenticated API calls
38- const authenticatedFetch = ( url : string , options : RequestInit = { } ) => {
39- const token = localStorage . getItem ( 'token' ) ;
38+ // Helper function for authenticated API calls with token refresh
39+ const authenticatedFetch = async ( url : string , options : RequestInit = { } ) => {
40+ // Ensure access token is valid (refresh if near expiry)
41+ await keycloakAuth . ensureValidToken ( ) ;
42+ let token = localStorage . getItem ( 'token' ) ;
4043 const headers : Record < string , string > = {
41- ...options . headers ,
42- 'Authorization' : `Bearer ${ token } ` ,
44+ ...options . headers as Record < string , string > ,
4345 } ;
44-
45- // Only set Content-Type for requests that have a body
46- if ( options . body ) {
47- headers [ 'Content-Type' ] = 'application/json' ;
46+ if ( token ) headers [ 'Authorization' ] = `Bearer ${ token } ` ;
47+ if ( options . body ) headers [ 'Content-Type' ] = headers [ 'Content-Type' ] || 'application/json' ;
48+
49+ let res = await fetch ( url , { ...options , headers } ) ;
50+ if ( res . status === 401 ) {
51+ // Try to refresh and retry once
52+ const refreshed = await keycloakAuth . refreshAccessToken ( ) ;
53+ token = localStorage . getItem ( 'token' ) ;
54+ const retryHeaders : Record < string , string > = { ...headers } ;
55+ if ( refreshed && token ) {
56+ retryHeaders [ 'Authorization' ] = `Bearer ${ token } ` ;
57+ res = await fetch ( url , { ...options , headers : retryHeaders } ) ;
58+ }
4859 }
49-
50- return fetch ( url , {
51- ...options ,
52- headers,
53- } ) ;
60+ return res ;
5461 } ;
5562
5663 // NLP parsing function
0 commit comments