Skip to content

Commit 1dc5d8b

Browse files
authored
Merge pull request #397 from vera/feat/get-custom-bearer-token
feat: add config setting "bearerTokenGetFunction"
2 parents 6c55a7c + 1cb805e commit 1dc5d8b

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/core/infra/repositories/ApiConfig.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ export class ApiConfig {
33
static dataverseApiAuthMechanism: DataverseApiAuthMechanism
44
static dataverseApiKey?: string
55
static bearerTokenLocalStorageKey?: string
6+
static bearerTokenGetFunction?: () => string | null
67

78
static init(
89
dataverseApiUrl: string,
910
dataverseApiAuthMechanism: DataverseApiAuthMechanism,
1011
dataverseApiKey?: string,
11-
bearerTokenLocalStorageKey?: string
12+
bearerTokenLocalStorageKey?: string,
13+
bearerTokenGetFunction?: () => string | null
1214
) {
1315
this.dataverseApiUrl = dataverseApiUrl
1416
this.dataverseApiAuthMechanism = dataverseApiAuthMechanism
1517
this.dataverseApiKey = dataverseApiKey
1618
this.bearerTokenLocalStorageKey = bearerTokenLocalStorageKey
19+
this.bearerTokenGetFunction = bearerTokenGetFunction
1720
}
1821
}
1922

src/core/infra/repositories/apiConfigBuilders.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ export const buildRequestConfig = (
4141
break
4242

4343
case DataverseApiAuthMechanism.BEARER_TOKEN: {
44-
if (!ApiConfig.bearerTokenLocalStorageKey) {
44+
if (!(ApiConfig.bearerTokenLocalStorageKey || ApiConfig.bearerTokenGetFunction)) {
4545
throw new Error(
46-
'Bearer token local storage key is not set in the ApiConfig, when using bearer token auth mechanism you must set the bearerTokenLocalStorageKey'
46+
'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'
4747
)
4848
}
4949

50-
const token = getLocalStorageItem<string>(ApiConfig.bearerTokenLocalStorageKey)
50+
let token
51+
52+
if (ApiConfig.bearerTokenLocalStorageKey) {
53+
token = getLocalStorageItem<string>(ApiConfig.bearerTokenLocalStorageKey)
54+
} else if (ApiConfig.bearerTokenGetFunction) {
55+
token = ApiConfig.bearerTokenGetFunction()
56+
}
5157

5258
if (token) {
5359
requestConfig.headers.Authorization = `Bearer ${token}`

0 commit comments

Comments
 (0)