pass headers to initializeApollo (nginx injects Authorization header to any request)

This commit is contained in:
vchikalkin 2024-05-21 14:50:28 +03:00
parent 13cd93338f
commit 16b7188773
5 changed files with 4 additions and 19 deletions

View File

@ -2,7 +2,6 @@
import { message } from '@/Components/Common/Notification';
import { publicRuntimeConfigSchema } from '@/config/schema/runtime-config';
import getUrls from '@/config/urls';
import { getToken } from '@/utils/auth';
import { ApolloLink, from, HttpLink } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
import { onError } from '@apollo/client/link/error';
@ -81,12 +80,10 @@ export function createLink(headers) {
}
if (isServer()) {
const token = getToken({ headers });
return {
headers: {
...existingHeaders,
authorization: `Bearer ${token}`,
authorization: headers.authorization,
},
};
}

View File

@ -29,7 +29,7 @@ export async function getServerSideProps({ req }) {
const { cookie = '' } = req.headers;
const queryClient = new QueryClient();
const apolloClient = initializeApollo(null, { cookie });
const apolloClient = initializeApollo(null, req.headers);
const getUserType = makeGetUserType({ apolloClient, queryClient });
try {

View File

@ -36,7 +36,7 @@ export async function getServerSideProps({ req }) {
const { cookie = '' } = req.headers;
const queryClient = new QueryClient();
const apolloClient = initializeApollo(null, { cookie });
const apolloClient = initializeApollo(null, req.headers);
const getUserType = makeGetUserType({ apolloClient, queryClient });
try {

View File

@ -37,7 +37,7 @@ export async function getServerSideProps({ req }) {
const { cookie = '' } = req.headers;
const queryClient = new QueryClient();
const apolloClient = initializeApollo(null, { cookie });
const apolloClient = initializeApollo(null, req.headers);
const getUserType = makeGetUserType({ apolloClient, queryClient });
try {

View File

@ -1,12 +0,0 @@
import type { IncomingHttpHeaders } from 'http';
type Request = {
headers: IncomingHttpHeaders;
};
export function getToken({ headers }: Request) {
return headers.cookie
?.split(';')
.find((c) => c.trim().startsWith('token='))
?.split('=')[1];
}