Skip to content

Commit

Permalink
fix: fix apollo headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Klesse committed Jun 13, 2024
1 parent 9bc34b9 commit 2bfa8b4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/runtime/plugins/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { getMainDefinition } from '@apollo/client/utilities';
import { provideApolloClient } from '@vue/apollo-composable';
import { createClient } from 'graphql-ws';
import { callWithNuxt, defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from 'nuxt/app';
import { callWithNuxt, defineNuxtPlugin, useNuxtApp, useRequestHeaders, useRuntimeConfig } from 'nuxt/app';

import { useAuth } from '../composables/use-auth';
import { useAuthState } from '../states/auth';
Expand All @@ -22,7 +22,7 @@ export default defineNuxtPlugin({
const nuxtApp = useNuxtApp();
const { host, wsUrl } = useRuntimeConfig().public;
const defaultClient = (nuxtApp.$apollo as any)?.defaultClient as unknown as ApolloClient<any>;

const defaultHeaders = useRequestHeaders();
if (!defaultClient) {
console.error('No defaultClient available');
return;
Expand Down Expand Up @@ -75,6 +75,9 @@ export default defineNuxtPlugin({
const authLink = setContext((_, { headers }) => {
const { accessTokenState, refreshTokenState } = useAuthState();
let token: string;

console.log(defaultHeaders, headers);

if (accessTokenState.value && refreshTokenState.value) {
if (_.operationName === 'refreshToken') {
token = refreshTokenState.value || null;
Expand All @@ -86,6 +89,7 @@ export default defineNuxtPlugin({
return {
headers: {
...headers,
...defaultHeaders,
Authorization: token ? `Bearer ${token}` : '', // Token in header
},
};
Expand Down

0 comments on commit 2bfa8b4

Please sign in to comment.