Skip to content
Open
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
11 changes: 8 additions & 3 deletions src/runtime/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { H3Event } from 'h3'
export async function _fetch<T>(
nuxt: ReturnType<typeof useNuxtApp>,
path: string,
fetchOptions?: Parameters<typeof $fetch>[1],
fetchOptions: Parameters<typeof $fetch>[1] = {},
proxyCookies = false
): Promise<T> {
// This fixes https://github.com/sidebase/nuxt-auth/issues/927
Expand All @@ -28,10 +28,16 @@ export async function _fetch<T>(
}
}

// Always include cookies to the request on browser by default
// https://github.com/sidebase/nuxt-auth/issues/1063
if (!fetchOptions.credentials) {
fetchOptions.credentials = 'include'
}

// Add browser cookies to the request on server when `proxyCookies` param is set
let event: H3Event | undefined
if (import.meta.server && proxyCookies) {
const fetchOptionsHeaders = new Headers(fetchOptions?.headers ?? {})
const fetchOptionsHeaders = new Headers(fetchOptions.headers ?? {})

event = await callWithNuxt(nuxt, useRequestEvent)

Expand All @@ -40,7 +46,6 @@ export async function _fetch<T>(
const cookies = event.node.req.headers.cookie
if (cookies) {
fetchOptionsHeaders.set('cookie', cookies)
fetchOptions ??= {}
fetchOptions.headers = fetchOptionsHeaders
}
}
Expand Down
Loading