Added refresh checking logic for tokens.
This commit is contained in:
@@ -16,6 +16,7 @@ import { ApolloLink } from "apollo-boost";
|
||||
import { ApolloProvider } from "react-apollo";
|
||||
import { persistCache } from "apollo-cache-persist";
|
||||
import initialState from "../graphql/initial-state";
|
||||
import { shouldRefreshToken, refreshToken } from "../graphql/middleware";
|
||||
|
||||
class AppContainer extends Component {
|
||||
state = {
|
||||
@@ -71,6 +72,11 @@ class AppContainer extends Component {
|
||||
const token = localStorage.getItem("token");
|
||||
// return the headers to the context so httpLink can read them
|
||||
if (token) {
|
||||
console.log("checking if token should refresh.");
|
||||
if (shouldRefreshToken) {
|
||||
refreshToken();
|
||||
}
|
||||
|
||||
return {
|
||||
headers: {
|
||||
...headers,
|
||||
|
||||
@@ -50,6 +50,8 @@ export default () => {
|
||||
|
||||
//add the bearer token to the headers.
|
||||
localStorage.setItem("token", token);
|
||||
const now = new Date();
|
||||
window.sessionStorage.setItem(`lastTokenRefreshTime`, now);
|
||||
|
||||
apolloClient
|
||||
.mutate({
|
||||
@@ -61,23 +63,6 @@ export default () => {
|
||||
console.log("User login upsert error.", error);
|
||||
});
|
||||
|
||||
// apolloClient
|
||||
// .query({
|
||||
// query: QUERY_BODYSHOP,
|
||||
// fetchPolicy: "network-only"
|
||||
// })
|
||||
// .then(r => {
|
||||
// const bodyShopData = r.data.bodyshops[0];
|
||||
// apolloClient.writeData({
|
||||
// data: {
|
||||
// bodyShopData: { ...bodyShopData, __typename: "bodyShopData" }
|
||||
// }
|
||||
// });
|
||||
// })
|
||||
// .catch(error => {
|
||||
// console.log("Error getting bodyshop data.", error);
|
||||
// });
|
||||
|
||||
apolloClient.writeData({
|
||||
data: {
|
||||
currentUser: {
|
||||
|
||||
Reference in New Issue
Block a user