Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin): take token from cookie even if proxyCookies is disabled #618

Open
wants to merge 1 commit into
base: v5
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApolloClientKeys, ApolloClient<any>>

Expand All @@ -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') {
Expand All @@ -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()

Expand All @@ -57,7 +59,7 @@ export default defineNuxtPlugin((nuxtApp) => {
return {
headers: {
...headers,
...(requestCookies && requestCookies),
...cookiesToProxy,
[clientConfig.authHeader!]: auth
}
}
Expand Down