Skip to content

Commit 02d17fc

Browse files
fix: generate basic auth header for client-side usage
1 parent 3ee9f0c commit 02d17fc

File tree

8 files changed

+25
-23
lines changed

8 files changed

+25
-23
lines changed

src/prefetch.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ofetch } from 'ofetch'
22
import type { KirbyQueryResponse } from 'kirby-types'
33
import { logger } from './kit'
4-
import { getAuthHeader } from './runtime/utils'
4+
import { createAuthHeader } from './runtime/utils'
55
import type { ModuleOptions } from './module'
66

77
export async function prefetchQueries(
@@ -13,7 +13,6 @@ export async function prefetchQueries(
1313
return results
1414

1515
const start = Date.now()
16-
const { auth, token, credentials } = options
1716

1817
for (const [key, query] of Object.entries(options.prefetch)) {
1918
const language = 'language' in query ? query.language : undefined
@@ -33,7 +32,7 @@ export async function prefetchQueries(
3332
method: 'POST',
3433
body: language ? query.query : query,
3534
headers: {
36-
...getAuthHeader({ auth, token, credentials }),
35+
...createAuthHeader(options),
3736
...(language && { 'X-Language': language }),
3837
},
3938
}),

src/runtime/composables/$kirby.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { hash } from 'ohash'
33
import type { NitroFetchOptions } from 'nitropack'
44
import type { ModuleOptions } from '../../module'
55
import type { ServerFetchOptions } from '../types'
6-
import { getAuthHeader, getProxyPath, headersToObject } from '../utils'
6+
import { createAuthHeader, getProxyPath, headersToObject } from '../utils'
77
import { useNuxtApp, useRuntimeConfig } from '#imports'
88

99
export type KirbyFetchOptions = Pick<
@@ -84,7 +84,7 @@ export function $kirby<T = any>(
8484
query,
8585
headers: {
8686
...baseHeaders,
87-
...getAuthHeader(kql),
87+
...createAuthHeader(kql),
8888
},
8989
method,
9090
body,

src/runtime/composables/$kql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { NitroFetchOptions } from 'nitropack'
33
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types'
44
import type { ModuleOptions } from '../../module'
55
import type { ServerFetchOptions } from '../types'
6-
import { getAuthHeader, getProxyPath, headersToObject } from '../utils'
6+
import { createAuthHeader, getProxyPath, headersToObject } from '../utils'
77
import { useNuxtApp, useRuntimeConfig } from '#imports'
88

99
export type KqlOptions = Pick<
@@ -64,7 +64,7 @@ export function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResp
6464
body: query,
6565
headers: {
6666
...baseHeaders,
67-
...getAuthHeader(kql),
67+
...createAuthHeader(kql),
6868
},
6969
}
7070

src/runtime/composables/useKirbyData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { AsyncData, AsyncDataOptions } from 'nuxt/app'
88
import { toValue } from '@vueuse/core'
99
import type { MaybeRefOrGetter } from '@vueuse/core'
1010
import type { ModuleOptions } from '../../module'
11-
import { getAuthHeader, getProxyPath, headersToObject } from '../utils'
11+
import { createAuthHeader, getProxyPath, headersToObject } from '../utils'
1212
import { useAsyncData, useRuntimeConfig } from '#imports'
1313

1414
type UseKirbyDataOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<
@@ -118,7 +118,7 @@ export function useKirbyData<T = any>(
118118
query,
119119
headers: {
120120
...headersToObject(headers),
121-
...getAuthHeader(kql),
121+
...createAuthHeader(kql),
122122
},
123123
method,
124124
body,

src/runtime/composables/useKql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types'
88
import { toValue } from '@vueuse/core'
99
import type { MaybeRefOrGetter } from '@vueuse/core'
1010
import type { ModuleOptions } from '../../module'
11-
import { getAuthHeader, getProxyPath, headersToObject } from '../utils'
11+
import { createAuthHeader, getProxyPath, headersToObject } from '../utils'
1212
import { useAsyncData, useRuntimeConfig } from '#imports'
1313

1414
export type UseKqlOptions<T> = Omit<AsyncDataOptions<T>, 'watch'> & Pick<
@@ -107,8 +107,8 @@ export function useKql<
107107
body: _query.value,
108108
headers: {
109109
...headersToObject(headers),
110+
...createAuthHeader(kql),
110111
...(_language.value && { 'X-Language': _language.value }),
111-
...getAuthHeader(kql),
112112
},
113113
})) as ResT
114114
}

src/runtime/server/handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createError, defineEventHandler, getRouterParam, readBody } from 'h3'
22
import type { FetchError } from 'ofetch'
33
import type { ModuleOptions } from '../../module'
4-
import { getAuthHeader } from '../utils'
4+
import { createAuthHeader } from '../utils'
55
import type { ServerFetchOptions } from '../types'
66

77
// @ts-expect-error: Will be resolved by Nitro
@@ -35,7 +35,7 @@ async function fetcher({
3535
}),
3636
headers: {
3737
...headers,
38-
...getAuthHeader(kql),
38+
...createAuthHeader(kql),
3939
},
4040
})
4141

src/runtime/server/imports.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { joinURL } from 'ufo'
22
import type { NitroFetchOptions } from 'nitropack'
33
import type { KirbyQueryRequest, KirbyQueryResponse } from 'kirby-types'
4-
import { getAuthHeader, headersToObject } from '../utils'
4+
import { createAuthHeader, headersToObject } from '../utils'
55
import { useRuntimeConfig } from '#imports'
66

77
export type KirbyFetchOptions = Omit<
@@ -46,7 +46,7 @@ export function $kirby<T = any>(
4646
baseURL: kql.url,
4747
headers: {
4848
...headersToObject(headers),
49-
...getAuthHeader(kql),
49+
...createAuthHeader(kql),
5050
},
5151
}) as Promise<T>
5252
}
@@ -65,8 +65,8 @@ export function $kql<T extends KirbyQueryResponse<any, boolean> = KirbyQueryResp
6565
body: query,
6666
headers: {
6767
...headersToObject(headers),
68+
...createAuthHeader(kql),
6869
...(language && { 'X-Language': language }),
69-
...getAuthHeader(kql),
7070
},
7171
}) as Promise<T>
7272
}

src/runtime/utils.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@ export function headersToObject(headers: HeadersInit = {}): Record<string, strin
1111
return headers
1212
}
1313

14-
export function getAuthHeader({
14+
export function createAuthHeader({
1515
auth,
1616
token,
1717
credentials,
1818
}: Pick<ModuleOptions, 'token' | 'credentials'> & { auth?: string }) {
19-
const headers: Record<string, string> = {}
20-
2119
if (auth === 'basic' && credentials) {
2220
const { username, password } = credentials
23-
headers.Authorization = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`
21+
let encoded = ''
22+
23+
if (process.server)
24+
encoded = Buffer.from(`${username}:${password}`).toString('base64')
25+
else if (process.client)
26+
encoded = btoa(`${username}:${password}`)
27+
28+
return { Authorization: `Basic ${encoded}` }
2429
}
2530

2631
if (auth === 'bearer')
27-
headers.Authorization = `Bearer ${token}`
28-
29-
return headers
32+
return { Authorization: `Bearer ${token}` }
3033
}

0 commit comments

Comments
 (0)