Skip to content

Commit

Permalink
fix: header auth
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Oct 24, 2024
1 parent 3786f61 commit 7a3aab6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/fetch/src/fetch.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ export const $fetch = createFetch({
headers: globalConfigureHeader,
onRequest(context) {
const token = getToken()
const headers: any = context.options.headers ?? {}
// eslint-disable-next-line prefer-destructuring
let headers: any = context.options.headers
if (headers && headers instanceof Headers) {
headers = Object.fromEntries(headers.entries())
} else {
headers = {}
}

if (token) {
headers['Authorization'] = `bearer ${token}`
headers.Authorization = `bearer ${token}`
}

headers['x-session-uuid'] =
headers['X-Session-Uuid'] =
globalThis?.sessionStorage?.getItem(uuidStorageKey) ?? uuid

context.options.params ??= {}
Expand Down
8 changes: 7 additions & 1 deletion packages/fetch/src/fetch.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export const $fetch = createFetch({

const token = cookie.get(TokenKey)?.value

const headers: any = context.options.headers ?? {}
// eslint-disable-next-line prefer-destructuring
let headers: any = context.options.headers
if (headers && headers instanceof Headers) {
headers = Object.fromEntries(headers.entries())
} else {
headers = {}
}
if (token) {
headers['Authorization'] = `bearer ${token}`
}
Expand Down

0 comments on commit 7a3aab6

Please sign in to comment.