Files
wlan-cloud-ui/app/index.js
2020-06-08 19:53:14 -04:00

97 lines
2.6 KiB
JavaScript

import React from 'react';
import ReactDOM from 'react-dom';
import { Router } from 'react-router-dom';
import ApolloClient from 'apollo-boost';
import { ApolloProvider } from '@apollo/react-hooks';
import gql from 'graphql-tag';
import 'styles/antd.less';
import 'styles/index.scss';
import App from 'containers/App';
import { AUTH_TOKEN } from 'constants/index';
import { getItem, setItem, removeItem } from 'utils/localStorage';
import history from 'utils/history';
const API_URI =
process.env.NODE_ENV === 'development'
? 'http://localhost:4000/'
: 'https://wlan-graphql.zone3.lab.connectus.ai/';
const MOUNT_NODE = document.getElementById('root');
const REFRESH_TOKEN = gql`
mutation UpdateToken($refreshToken: String!) {
updateToken(refreshToken: $refreshToken) {
access_token
refresh_token
expires_in
}
}
`;
const client = new ApolloClient({
uri: API_URI,
request: operation => {
const token = getItem(AUTH_TOKEN);
operation.setContext({
headers: {
authorization: token ? `Bearer ${token.access_token}` : '',
},
});
},
onError: ({ graphQLErrors, operation, forward }) => {
if (graphQLErrors) {
graphQLErrors.forEach(err => {
// handle errors differently based on its error code
switch (err.extensions.code) {
case 'FORBIDDEN':
case 'UNAUTHENTICATED':
operation.setContext({
headers: {
...operation.getContext().headers,
authorization: client
.mutate({
mutation: REFRESH_TOKEN,
variables: {
refreshToken: getItem(AUTH_TOKEN).refresh_token,
},
})
.then(({ data }) => {
setItem(AUTH_TOKEN, data.updateToken, data.updateToken.expires_in);
return data.updateToken;
}),
},
});
return forward(operation);
case 'INTERNAL_SERVER_ERROR':
if (err.path && err.path[0] === 'updateToken') {
removeItem(AUTH_TOKEN);
history.push('/login');
}
return forward(operation);
default:
return forward(operation);
}
});
}
},
});
const render = () => {
ReactDOM.render(
<Router history={history}>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</Router>,
MOUNT_NODE
);
};
if (process.env.NODE_ENV !== 'production' && module.hot) {
module.hot.accept('containers/App', () => ReactDOM.unmountComponentAtNode(MOUNT_NODE));
}
render();