From ec5d2679ce582bbc46d76d5739275b182dd76c94 Mon Sep 17 00:00:00 2001 From: Andrei Luca <1881266+iamandrewluca@users.noreply.github.com> Date: Wed, 8 May 2024 18:41:21 +0300 Subject: [PATCH] fix(plugin): take token from cookie even if proxyCookies is disabled --- src/runtime/plugin.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 9263dbc1..e775386d 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -16,7 +16,7 @@ import { NuxtApollo } from '#apollo' import type { ApolloClientKeys } from '#apollo' export default defineNuxtPlugin((nuxtApp) => { - const requestCookies = (process.server && NuxtApollo.proxyCookies && useRequestHeaders(['cookie'])) || undefined + const requestCookies = process.server ? useRequestHeaders(['cookie']) : {} const clients = {} as Record> @@ -31,8 +31,8 @@ export default defineNuxtPlugin((nuxtApp) => { if (process.client) { const t = useCookie(clientConfig.tokenName!).value if (t) { token.value = t } - } else if (requestCookies?.cookie) { - const t = requestCookies.cookie.split(';').find(c => c.trim().startsWith(`${clientConfig.tokenName}=`))?.split('=')?.[1] + } else if (process.server) { + const t = requestCookies.cookie?.split(';').find(c => c.trim().startsWith(`${clientConfig.tokenName}=`))?.split('=')?.[1] if (t) { token.value = t } } } else if (process.client && clientConfig.tokenStorage === 'localStorage') { @@ -49,6 +49,8 @@ export default defineNuxtPlugin((nuxtApp) => { return `${clientConfig?.authType} ${token.value}` } + const cookiesToProxy = (process.server && NuxtApollo.proxyCookies) ? requestCookies : {} + const authLink = setContext(async (_, { headers }) => { const auth = await getAuth() @@ -57,7 +59,7 @@ export default defineNuxtPlugin((nuxtApp) => { return { headers: { ...headers, - ...(requestCookies && requestCookies), + ...cookiesToProxy, [clientConfig.authHeader!]: auth } }