Skip to content

Commit 97faa2d

Browse files
committed
fix(auth): Propagate cookie for SSR authentication
Ensures the `auth_token` cookie is correctly forwarded during Server-Side Rendering (SSR) when fetching user data. This addresses an issue where cookies were not reliably propagated, preventing proper authentication for the `/me` endpoint on SSR pages. Explicitly reads and attaches the `cookie` header to the fetch request.
1 parent 83b7f8d commit 97faa2d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/runtime/composables/useAuthentication.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useRuntimeConfig, useFetch } from '#app'
1+
import { useState, useRuntimeConfig, useFetch, useRequestHeaders } from '#app'
22
import { computed, readonly } from 'vue'
33
import type { User, UserWithoutPassword } from 'nuxt-users/utils'
44

@@ -59,10 +59,13 @@ export const useAuthentication = () => {
5959
let userData: UserWithoutPassword | null = null
6060

6161
if (useSSR) {
62-
// Use useFetch for SSR - properly handles cookies
62+
// Use useFetch for SSR - must forward cookie header so /me can read auth_token.
63+
// useRequestFetch does not always propagate in plugin->composable->useFetch chain.
64+
const headers = useRequestHeaders(['cookie'])
6365
const { data, error } = await useFetch<{ user: UserWithoutPassword }>(`${apiBasePath}/me`, {
6466
server: true,
65-
lazy: false
67+
lazy: false,
68+
...(Object.keys(headers).length ? { headers } : {})
6669
})
6770

6871
if (error.value) {

0 commit comments

Comments
 (0)