Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core/infra/repositories/ApiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/core/infra/repositories/apiConfigBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(ApiConfig.bearerTokenLocalStorageKey)
let token

if (ApiConfig.bearerTokenLocalStorageKey) {
token = getLocalStorageItem<string>(ApiConfig.bearerTokenLocalStorageKey)
} else if (ApiConfig.bearerTokenGetFunction) {
token = ApiConfig.bearerTokenGetFunction()
}

if (token) {
requestConfig.headers.Authorization = `Bearer ${token}`
Expand Down