Skip to content

Commit

Permalink
Fix token expiration
Browse files Browse the repository at this point in the history
Solves #1149 and adds a 7 day token expiration to woo-session
  • Loading branch information
w3bdesign committed Oct 28, 2023
1 parent 1fa4cf8 commit 00fde05
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/utils/apollo/ApolloClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,39 @@ import {
ApolloLink,
} from '@apollo/client';

const SEVEN_DAYS = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds

/**
* Middleware operation
* If we have a session token in localStorage, add it to the GraphQL request as a Session header.
*/
export const middleware = new ApolloLink((operation, forward) => {
/**
* If session data exist in local storage, set value as session header.
* Here we also delete the session if it is older than 24 hours
* Here we also delete the session if it is older than 7 days
*/
const session = process.browser ? localStorage.getItem('woo-session') : null;
const sessionData = process.browser
? JSON.parse(localStorage.getItem('woo-session'))
: null;

if (sessionData) {
const { token, createdTime } = sessionData;

if (session) {
operation.setContext(() => ({
headers: {
'woocommerce-session': `Session ${session}`,
},
}));
// Check if the token is older than 7 days
if (Date.now() - createdTime > SEVEN_DAYS) {
// If it is, delete it
localStorage.removeItem('woo-session');
localStorage.setItem('woocommerce-cart', JSON.stringify({}));
} else {
// If it's not, use the token
operation.setContext(() => ({
headers: {
'woocommerce-session': `Session ${token}`,
},
}));
}
}

return forward(operation);
});

Expand All @@ -51,7 +66,10 @@ export const afterware = new ApolloLink((operation, forward) =>
localStorage.removeItem('woo-session');
// Update session new data if changed.
} else if (!localStorage.getItem('woo-session')) {
localStorage.setItem('woo-session', session);
localStorage.setItem(
'woo-session',
JSON.stringify({ token: session, createdTime: Date.now() }),
);
}
}

Expand Down

0 comments on commit 00fde05

Please sign in to comment.