Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
w3bdesign committed Feb 2, 2022
1 parent 1b558f2 commit d64c926
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions utils/apollo/ApolloClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,29 @@ export const middleware = new ApolloLink((operation, forward) => {
*
* This catches the incoming session token and stores it in localStorage, for future GraphQL requests.
*/
export const afterware = new ApolloLink((operation, forward) => {
return forward(operation).map((response) => {
/**
* Check for session header and update session in local storage accordingly.
*/
const context = operation.getContext();
const {
response: { headers },
} = context;
export const afterware = new ApolloLink((operation, forward) => forward(operation).map((response) => {
/**
* Check for session header and update session in local storage accordingly.
*/
const context = operation.getContext();
const {
response: { headers },
} = context;

const session = headers.get('woocommerce-session');
const session = headers.get('woocommerce-session');

if (session && process.browser) {
// Remove session data if session destroyed.
if ('false' === session) {
localStorage.removeItem('woo-session');
// Update session new data if changed.
} else if (localStorage.getItem('woo-session') !== session) {
localStorage.setItem('woo-session', headers.get('woocommerce-session'));
localStorage.setItem('woo-session-expiry', new Date());
}
if (session && process.browser) {
// Remove session data if session destroyed.
if ('false' === session) {
localStorage.removeItem('woo-session');
// Update session new data if changed.
} else if (localStorage.getItem('woo-session') !== session) {
localStorage.setItem('woo-session', headers.get('woocommerce-session'));
localStorage.setItem('woo-session-expiry', new Date());
}
return response;
});
});
}
return response;
}));

const clientSide = typeof window === 'undefined';

Expand Down

0 comments on commit d64c926

Please sign in to comment.