Skip to content

Commit 45bef15

Browse files
committed
fix(#1063): always include browser cookies by default
1 parent b955bea commit 45bef15

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/runtime/utils/fetch.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { H3Event } from 'h3'
88
export async function _fetch<T>(
99
nuxt: ReturnType<typeof useNuxtApp>,
1010
path: string,
11-
fetchOptions?: Parameters<typeof $fetch>[1],
11+
fetchOptions: Parameters<typeof $fetch>[1] = {},
1212
proxyCookies = false
1313
): Promise<T> {
1414
// This fixes https://github.com/sidebase/nuxt-auth/issues/927
@@ -28,10 +28,16 @@ export async function _fetch<T>(
2828
}
2929
}
3030

31+
// Always include cookies to the request on browser by default
32+
// https://github.com/sidebase/nuxt-auth/issues/1063
33+
if (!fetchOptions.credentials) {
34+
fetchOptions.credentials = 'include'
35+
}
36+
3137
// Add browser cookies to the request on server when `proxyCookies` param is set
3238
let event: H3Event | undefined
3339
if (import.meta.server && proxyCookies) {
34-
const fetchOptionsHeaders = new Headers(fetchOptions?.headers ?? {})
40+
const fetchOptionsHeaders = new Headers(fetchOptions.headers ?? {})
3541

3642
event = await callWithNuxt(nuxt, useRequestEvent)
3743

@@ -40,7 +46,6 @@ export async function _fetch<T>(
4046
const cookies = event.node.req.headers.cookie
4147
if (cookies) {
4248
fetchOptionsHeaders.set('cookie', cookies)
43-
fetchOptions ??= {}
4449
fetchOptions.headers = fetchOptionsHeaders
4550
}
4651
}

0 commit comments

Comments
 (0)