2020-04-10 04:40:22 +02:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2020-06-15 02:50:35 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
import createAuthRefreshInterceptor from 'axios-auth-refresh';
|
2020-07-17 02:40:23 +02:00
|
|
|
import { ApolloProvider } from '@apollo/react-hooks';
|
|
|
|
import { ApolloClient } from 'apollo-client';
|
|
|
|
import { HttpLink } from 'apollo-link-http';
|
|
|
|
import { onError } from 'apollo-link-error';
|
2020-08-01 03:01:14 +02:00
|
|
|
import { enableMapSet } from 'immer';
|
2020-07-17 02:40:23 +02:00
|
|
|
import { ApolloLink, Observable, fromPromise } from 'apollo-link';
|
2020-10-01 21:17:43 +02:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import updateLocale from 'dayjs/plugin/updateLocale';
|
|
|
|
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
|
|
|
|
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
|
|
|
import isBetween from 'dayjs/plugin/isBetween';
|
|
|
|
import weekday from 'dayjs/plugin/weekday';
|
2020-07-17 02:40:23 +02:00
|
|
|
import { getAccessToken, getNewToken, setAccessToken } from 'shared/utils/accessToken';
|
2020-08-01 03:01:14 +02:00
|
|
|
import cache from './App/cache';
|
2020-04-10 04:40:22 +02:00
|
|
|
import App from './App';
|
|
|
|
|
2020-06-15 02:50:35 +02:00
|
|
|
// https://able.bio/AnasT/apollo-graphql-async-access-token-refresh--470t1c8
|
2020-10-01 21:17:43 +02:00
|
|
|
|
|
|
|
dayjs.extend(isSameOrAfter);
|
|
|
|
|
|
|
|
dayjs.extend(weekday);
|
|
|
|
dayjs.extend(isBetween);
|
|
|
|
dayjs.extend(customParseFormat);
|
2020-08-01 03:01:14 +02:00
|
|
|
enableMapSet();
|
2020-06-15 02:50:35 +02:00
|
|
|
|
2020-10-01 21:17:43 +02:00
|
|
|
dayjs.extend(updateLocale);
|
|
|
|
|
|
|
|
dayjs.updateLocale('en', {
|
2020-08-28 01:57:23 +02:00
|
|
|
week: {
|
|
|
|
dow: 1, // First day of week is Monday
|
|
|
|
doy: 7, // First week of year must contain 1 January (7 + 1 - 1)
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-15 02:50:35 +02:00
|
|
|
let forward$;
|
|
|
|
let isRefreshing = false;
|
|
|
|
let pendingRequests: any = [];
|
|
|
|
|
2020-06-13 00:21:58 +02:00
|
|
|
const refreshAuthLogic = (failedRequest: any) =>
|
2020-07-17 02:40:23 +02:00
|
|
|
axios.post('/auth/refresh_token', {}, { withCredentials: true }).then(tokenRefreshResponse => {
|
2020-06-13 00:21:58 +02:00
|
|
|
setAccessToken(tokenRefreshResponse.data.accessToken);
|
|
|
|
failedRequest.response.config.headers.Authorization = `Bearer ${tokenRefreshResponse.data.accessToken}`;
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
createAuthRefreshInterceptor(axios, refreshAuthLogic);
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
const resolvePendingRequests = () => {
|
|
|
|
pendingRequests.map((callback: any) => callback());
|
|
|
|
pendingRequests = [];
|
|
|
|
};
|
|
|
|
|
2020-04-10 22:31:12 +02:00
|
|
|
const resolvePromise = (resolve: () => void) => {
|
|
|
|
pendingRequests.push(() => resolve());
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetPendingRequests = () => {
|
|
|
|
pendingRequests = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
const setRefreshing = (newVal: boolean) => {
|
|
|
|
isRefreshing = newVal;
|
|
|
|
};
|
|
|
|
|
2020-07-17 02:40:23 +02:00
|
|
|
const errorLink = onError(({ graphQLErrors, networkError, operation, forward }) => {
|
2020-04-10 04:40:22 +02:00
|
|
|
if (graphQLErrors) {
|
|
|
|
for (const err of graphQLErrors) {
|
2020-04-10 22:31:12 +02:00
|
|
|
if (err.extensions && err.extensions.code) {
|
|
|
|
switch (err.extensions.code) {
|
|
|
|
case 'UNAUTHENTICATED':
|
|
|
|
if (!isRefreshing) {
|
|
|
|
setRefreshing(true);
|
|
|
|
forward$ = fromPromise(
|
|
|
|
getNewToken()
|
|
|
|
.then((response: any) => {
|
|
|
|
setAccessToken(response.accessToken);
|
|
|
|
resolvePendingRequests();
|
|
|
|
return response.accessToken;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
resetPendingRequests();
|
|
|
|
// TODO
|
|
|
|
// Handle token refresh errors e.g clear stored tokens, redirect to login, ...
|
|
|
|
return undefined;
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setRefreshing(false);
|
|
|
|
}),
|
|
|
|
).filter(value => Boolean(value));
|
|
|
|
} else {
|
|
|
|
forward$ = fromPromise(new Promise(resolvePromise));
|
|
|
|
}
|
|
|
|
return forward$.flatMap(() => forward(operation));
|
|
|
|
default:
|
|
|
|
// pass
|
|
|
|
}
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (networkError) {
|
2020-08-23 19:27:56 +02:00
|
|
|
console.log(`[Network error]: ${networkError}`); // eslint-disable-line no-console
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
2020-04-10 22:31:12 +02:00
|
|
|
return undefined;
|
2020-04-10 04:40:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const requestLink = new ApolloLink(
|
|
|
|
(operation, forward) =>
|
|
|
|
new Observable((observer: any) => {
|
|
|
|
let handle: any;
|
|
|
|
Promise.resolve(operation)
|
2020-04-10 22:31:12 +02:00
|
|
|
.then((op: any) => {
|
2020-04-10 04:40:22 +02:00
|
|
|
const accessToken = getAccessToken();
|
|
|
|
if (accessToken) {
|
2020-04-10 22:31:12 +02:00
|
|
|
op.setContext({
|
2020-04-10 04:40:22 +02:00
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${accessToken}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
handle = forward(operation).subscribe({
|
|
|
|
next: observer.next.bind(observer),
|
|
|
|
error: observer.error.bind(observer),
|
|
|
|
complete: observer.complete.bind(observer),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(observer.error.bind(observer));
|
|
|
|
|
|
|
|
return () => {
|
2020-04-10 22:31:12 +02:00
|
|
|
if (handle) {
|
|
|
|
handle.unsubscribe();
|
|
|
|
}
|
2020-04-10 04:40:22 +02:00
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const client = new ApolloClient({
|
|
|
|
link: ApolloLink.from([
|
2020-07-17 02:40:23 +02:00
|
|
|
onError(({ graphQLErrors, networkError }) => {
|
2020-04-10 22:31:12 +02:00
|
|
|
if (graphQLErrors) {
|
2020-08-23 19:27:56 +02:00
|
|
|
graphQLErrors.forEach(
|
|
|
|
({ message, locations, path }) =>
|
|
|
|
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`), // eslint-disable-line no-console
|
2020-04-10 04:40:22 +02:00
|
|
|
);
|
2020-04-10 22:31:12 +02:00
|
|
|
}
|
|
|
|
if (networkError) {
|
2020-08-23 19:27:56 +02:00
|
|
|
console.log(`[Network error]: ${networkError}`); // eslint-disable-line no-console
|
2020-04-10 22:31:12 +02:00
|
|
|
}
|
2020-04-10 04:40:22 +02:00
|
|
|
}),
|
|
|
|
errorLink,
|
|
|
|
requestLink,
|
|
|
|
new HttpLink({
|
2020-07-16 01:20:08 +02:00
|
|
|
uri: '/graphql',
|
2020-04-10 04:40:22 +02:00
|
|
|
credentials: 'same-origin',
|
|
|
|
}),
|
|
|
|
]),
|
2020-08-01 03:01:14 +02:00
|
|
|
cache,
|
2020-04-10 04:40:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
<ApolloProvider client={client}>
|
|
|
|
<App />
|
|
|
|
</ApolloProvider>,
|
|
|
|
document.getElementById('root'),
|
|
|
|
);
|