diff --git a/src/core/infra/repositories/ApiConfig.ts b/src/core/infra/repositories/ApiConfig.ts index dd0144ca..04444b2b 100644 --- a/src/core/infra/repositories/ApiConfig.ts +++ b/src/core/infra/repositories/ApiConfig.ts @@ -3,17 +3,20 @@ export class ApiConfig { static dataverseApiAuthMechanism: DataverseApiAuthMechanism static dataverseApiKey?: string static bearerTokenLocalStorageKey?: string + static bearerTokenGetFunction?: () => string | null static init( dataverseApiUrl: string, dataverseApiAuthMechanism: DataverseApiAuthMechanism, dataverseApiKey?: string, - bearerTokenLocalStorageKey?: string + bearerTokenLocalStorageKey?: string, + bearerTokenGetFunction?: () => string ) { this.dataverseApiUrl = dataverseApiUrl this.dataverseApiAuthMechanism = dataverseApiAuthMechanism this.dataverseApiKey = dataverseApiKey this.bearerTokenLocalStorageKey = bearerTokenLocalStorageKey + this.bearerTokenGetFunction = bearerTokenGetFunction } } diff --git a/src/core/infra/repositories/apiConfigBuilders.ts b/src/core/infra/repositories/apiConfigBuilders.ts index c245c58e..5030b0b1 100644 --- a/src/core/infra/repositories/apiConfigBuilders.ts +++ b/src/core/infra/repositories/apiConfigBuilders.ts @@ -41,13 +41,19 @@ export const buildRequestConfig = ( break case DataverseApiAuthMechanism.BEARER_TOKEN: { - if (!ApiConfig.bearerTokenLocalStorageKey) { + if (!(ApiConfig.bearerTokenLocalStorageKey || ApiConfig.bearerTokenGetFunction)) { throw new Error( - 'Bearer token local storage key is not set in the ApiConfig, when using bearer token auth mechanism you must set the bearerTokenLocalStorageKey' + 'Bearer token local storage key or get function is not set in the ApiConfig, when using bearer token auth mechanism you must set the bearerTokenLocalStorageKey or bearerTokenGetFunction' ) } - const token = getLocalStorageItem(ApiConfig.bearerTokenLocalStorageKey) + let token + + if (ApiConfig.bearerTokenLocalStorageKey) { + token = getLocalStorageItem(ApiConfig.bearerTokenLocalStorageKey) + } else if (ApiConfig.bearerTokenGetFunction) { + token = ApiConfig.bearerTokenGetFunction() + } if (token) { requestConfig.headers.Authorization = `Bearer ${token}`