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 2367c9d commit 73f9db3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ export default defineNuxtModule<ModuleOptions>({
},
},
httpEndpoint: options.host || null,
httpLinkOptions: {
credentials: 'include',
fetchOptions: {
credentials: 'include',
},
},
proxyCookies: true,
tokenName: `apollo:${options.storagePrefix}.token`,
tokenStorage: 'cookie',
Expand Down
54 changes: 37 additions & 17 deletions src/runtime/plugins/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ApolloClient } from '@apollo/client/core';

import { ApolloLink, HttpLink, from, fromPromise, split } from '@apollo/client/core';
import { HttpLink, from, fromPromise, split } from '@apollo/client/core';
import { setContext } from '@apollo/client/link/context';
import { onError } from '@apollo/client/link/error';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { getMainDefinition } from '@apollo/client/utilities';
Expand All @@ -18,7 +19,6 @@ export default defineNuxtPlugin({
dependsOn: ['cookies'],
name: 'apollo',
async setup() {
const links = [];
const nuxtApp = useNuxtApp();
const { host, wsUrl } = useRuntimeConfig().public;
const defaultClient = (nuxtApp.$apollo as any)?.defaultClient as unknown as ApolloClient<any>;
Expand Down Expand Up @@ -72,29 +72,49 @@ export default defineNuxtPlugin({
}
});

const authMiddleware = new ApolloLink((operation, forward) => {
const headers: Record<string, string> = {};
const operationName = (operation.query.definitions[0] as any)?.selectionSet?.selections[0]?.name?.value;
const authLink = setContext((_, { headers }) => {
const { accessTokenState, refreshTokenState } = useAuthState();

let token: string;
if (accessTokenState.value && refreshTokenState.value) {
let token: string;

if (operationName === 'refreshToken') {
if (_.operationName === 'refreshToken') {
token = refreshTokenState.value || null;
} else {
token = accessTokenState.value || null;
}

if (token) {
headers.Authorization = 'Bearer ' + token;
}

operation.setContext(() => ({ headers }));
}
return forward(operation);

return {
headers: {
...headers,
Authorization: token ? `Bearer ${token}` : '', // Token in header
},
};
});

// const authMiddleware = new ApolloLink((operation, forward) => {
// const headers: Record<string, string> = {};
// console.log('headers', headers);
// const operationName = (operation.query.definitions[0] as any)?.selectionSet?.selections[0]?.name?.value;
// const { accessTokenState, refreshTokenState } = useAuthState();
//
// if (accessTokenState.value && refreshTokenState.value) {
// let token: string;
//
// if (operationName === 'refreshToken') {
// token = refreshTokenState.value || null;
// } else {
// token = accessTokenState.value || null;
// }
//
// if (token) {
// headers.Authorization = 'Bearer ' + token;
// }
//
// operation.setContext(() => ({ headers }));
// }
// return forward(operation);
// });

const httpLink = new HttpLink({
credentials: 'include',
uri: (host as string) || '',
Expand Down Expand Up @@ -130,7 +150,7 @@ export default defineNuxtPlugin({
: httpLink;
/* eslint-enable */

defaultClient.setLink(from([authMiddleware, errorLink, splitLink]));
defaultClient.setLink(from([authLink, errorLink, splitLink]));

provideApolloClient(defaultClient);
},
Expand Down

0 comments on commit 73f9db3

Please sign in to comment.