apps/web: pass DEV_AUTH_TOKEN to GQL request
This commit is contained in:
parent
ef4f7b2547
commit
7d5be83e4c
@ -1,9 +1,12 @@
|
||||
/* eslint-disable sonarjs/cognitive-complexity */
|
||||
import { message } from '@/Components/Common/Notification';
|
||||
import { publicRuntimeConfigSchema } from '@/config/schema/runtime-config';
|
||||
import getUrls from '@/config/urls';
|
||||
import { ApolloLink, from, HttpLink } from '@apollo/client';
|
||||
import { setContext } from '@apollo/client/link/context';
|
||||
import { onError } from '@apollo/client/link/error';
|
||||
import { getCurrentScope } from '@sentry/nextjs';
|
||||
import getConfig from 'next/config';
|
||||
import { isServer } from 'tools';
|
||||
|
||||
const { URL_CRM_GRAPHQL } = getUrls();
|
||||
@ -61,6 +64,27 @@ const httpLink = new HttpLink({
|
||||
uri: URL_CRM_GRAPHQL,
|
||||
});
|
||||
|
||||
const authLink = setContext((_, { headers: existingHeaders }) => {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
const { publicRuntimeConfig } = getConfig();
|
||||
const { DEV_AUTH_TOKEN } = publicRuntimeConfigSchema.parse(publicRuntimeConfig);
|
||||
|
||||
if (DEV_AUTH_TOKEN)
|
||||
return {
|
||||
headers: {
|
||||
...existingHeaders,
|
||||
authorization: `Bearer ${DEV_AUTH_TOKEN}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
headers: {
|
||||
...existingHeaders,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const key = 'APOLLO_GRAPHQL';
|
||||
|
||||
const errorLink = onError(({ graphQLErrors, networkError, operation, response }) => {
|
||||
@ -83,4 +107,4 @@ const errorLink = onError(({ graphQLErrors, networkError, operation, response })
|
||||
});
|
||||
});
|
||||
|
||||
export const link = from([errorLink, modifyDataLink, httpLink]);
|
||||
export const link = from([authLink, errorLink, modifyDataLink, httpLink]);
|
||||
|
||||
@ -11,6 +11,7 @@ const envSchema = z.object({
|
||||
URL_CACHE_GET_QUERIES_DIRECT: z.string().default('http://api:3001/proxy/get-queries'),
|
||||
URL_CACHE_GET_QUERY_DIRECT: z.string().default('http://api:3001/proxy/get-query'),
|
||||
URL_CACHE_RESET_QUERIES_DIRECT: z.string().default('http://api:3001/proxy/reset'),
|
||||
DEV_AUTH_TOKEN: z.string().optional(),
|
||||
URL_CORE_CALCULATE_DIRECT: z.string(),
|
||||
URL_CORE_FINGAP_DIRECT: z.string(),
|
||||
URL_CRM_CREATEKP_DIRECT: z.string(),
|
||||
|
||||
@ -2,6 +2,7 @@ const envSchema = require('./env');
|
||||
|
||||
const publicRuntimeConfigSchema = envSchema.pick({
|
||||
BASE_PATH: true,
|
||||
DEV_AUTH_TOKEN: true,
|
||||
SENTRY_DSN: true,
|
||||
SENTRY_ENVIRONMENT: true,
|
||||
USE_DEV_COLORS: true,
|
||||
@ -9,6 +10,7 @@ const publicRuntimeConfigSchema = envSchema.pick({
|
||||
|
||||
const serverRuntimeConfigSchema = envSchema.pick({
|
||||
BASE_PATH: true,
|
||||
DEV_AUTH_TOKEN: true,
|
||||
PORT: true,
|
||||
SENTRY_DSN: true,
|
||||
SENTRY_ENVIRONMENT: true,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user