Skip to content

Commit ab6e3fc

Browse files
committed
add barrar token to graphql and axios
1 parent d3463a3 commit ab6e3fc

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/client.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import axios from 'axios';
2+
import KeycloakServices from 'keycloak/keycloakServices';
23

34
const client = axios.create();
45

6+
client.interceptors.request.use(
7+
config => {
8+
const token = KeycloakServices.getToken();
9+
if (token) {
10+
return {
11+
...config,
12+
headers: {
13+
...config.headers,
14+
Authorization: `Bearer ${token}`,
15+
},
16+
};
17+
}
18+
return config;
19+
},
20+
error => Promise.reject(error)
21+
);
22+
523
export default client;

src/graphql/useApolloClient.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
ApolloLink,
66
useReactiveVar,
77
} from '@apollo/client';
8+
import { setContext } from '@apollo/client/link/context';
89
import { onError } from '@apollo/client/link/error';
910
import { selectors } from 'reducers';
1011
import { useSelector } from 'react-redux';
1112
import cache, { numberErrorGraphQLVar } from 'cache';
1213
import { Modal } from 'antd';
1314
import { GrafanaLink } from 'components';
15+
import KeycloakServices from 'keycloak/keycloakServices';
1416

1517
const MAX_ERRORS_THRESHOLD = 5; // Maximum number of errors allowed within the time interval
1618
const TIME_INTERVAL = 60000; // 60 seconds in milliseconds
@@ -53,8 +55,18 @@ const useApolloClient = () => {
5355
uri: `${backendApiUrl.replace('/api/v1', '')}/graphql`,
5456
});
5557

58+
const authLink = setContext((_, { headers }) => {
59+
const token = KeycloakServices.getToken();
60+
return {
61+
headers: {
62+
...headers,
63+
Authorization: token ? `Bearer ${token}` : '',
64+
},
65+
};
66+
});
67+
5668
const apolloClient = new ApolloClient({
57-
link: ApolloLink.from([errorLink, httpLink]),
69+
link: ApolloLink.from([authLink, errorLink, httpLink]),
5870
cache,
5971
});
6072

src/keycloak/keycloakServices.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,27 @@ import Keycloak from 'keycloak-js';
44
const KeycloakConfig = {
55
clientId: 'simulator-ui-app',
66
realm: 'Hkube',
7-
url: 'https://cicd.hkube.org/hkube/keycloak/auth',
8-
// resource: 'simulator-ui-app',
7+
'ssl-required': 'external',
8+
url: 'https://cicd.hkube.org/hkube/keycloak',
9+
resource: 'simulator-ui-app',
910
enableCors: true,
1011
allowedOrigins: '*',
11-
clientUId: '23c95f69-17a2-4b9d-9001-39a5cb8f05fb',
12+
'public-client': true,
13+
'confidential-port': 0,
14+
clientUId: '7a177d05-5441-4ced-a236-ed51b8525da6',
1215
checkLoginIframe: true,
1316
checkLoginIframeInterval: 30,
1417
};
1518

19+
/*
20+
"realm": "Hkube",
21+
"auth-server-url": "https://cicd.hkube.org/hkube/keycloak",
22+
"ssl-required": "external",
23+
"resource": "simulator-ui-app",
24+
"public-client": true,
25+
"confidential-port": 0
26+
*/
27+
1628
const _kc = new Keycloak(KeycloakConfig);
1729

1830
const initKeycloak = (appToRender, renderError) => {

0 commit comments

Comments
 (0)