Skip to content

Commit 31ae05c

Browse files
committed
feat: handle woo-session for carts
1 parent 6dcfe42 commit 31ae05c

File tree

1 file changed

+78
-14
lines changed

1 file changed

+78
-14
lines changed
Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpLink, from } from '@apollo/client';
1+
import { ApolloLink, HttpLink, from } from '@apollo/client';
22
import {
33
NextSSRApolloClient,
44
NextSSRInMemoryCache,
@@ -7,29 +7,93 @@ import {
77
import { createErrorLink } from '../utils';
88
import { PUBLIC_GATEWAY_URL } from '@/config/app';
99

10+
const SEVEN_DAYS = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds
11+
const WOO_SESSION_KEY = 'woo-session';
12+
13+
/**
14+
* Woo Session setter operation.
15+
* If we have a session token in localStorage, add it to the GraphQL request as a Session header.
16+
*/
17+
export const wooSessionSetter = new ApolloLink((operation, forward) => {
18+
/**
19+
* If session data exist in local storage, set value as session header.
20+
* Here we also delete the session if it is older than 7 days
21+
*/
22+
const localStorageValue = localStorage.getItem(WOO_SESSION_KEY);
23+
const sessionData = localStorageValue ? JSON.parse(localStorageValue) : null;
24+
25+
if (sessionData && sessionData.token && sessionData.createdTime) {
26+
const { token, createdTime } = sessionData;
27+
28+
// Check if the token is older than 7 days
29+
if (Date.now() - createdTime > SEVEN_DAYS) {
30+
// If it is, delete it
31+
localStorage.removeItem(WOO_SESSION_KEY);
32+
localStorage.setItem('woocommerce-cart', JSON.stringify({}));
33+
} else {
34+
// If it's not, use the token
35+
operation.setContext(() => ({
36+
headers: {
37+
'woocommerce-session': `Session ${token}`,
38+
},
39+
}));
40+
}
41+
}
42+
43+
return forward(operation);
44+
});
45+
46+
/**
47+
* Woo Session updater operation.
48+
*
49+
* This catches the incoming session token and stores it in localStorage, for future GraphQL requests.
50+
*/
51+
export const wooSessionUpdater = new ApolloLink((operation, forward) =>
52+
forward(operation).map((response) => {
53+
/**
54+
* Check for session header and update session in local storage accordingly.
55+
*/
56+
const context = operation.getContext();
57+
const {
58+
response: { headers },
59+
} = context;
60+
61+
const session = headers.get('woocommerce-session');
62+
63+
if (session) {
64+
if ('false' === session) {
65+
// Remove session data if session destroyed.
66+
localStorage.removeItem(WOO_SESSION_KEY);
67+
// Update session new data if changed.
68+
} else if (!localStorage.getItem(WOO_SESSION_KEY)) {
69+
localStorage.setItem(
70+
WOO_SESSION_KEY,
71+
JSON.stringify({ token: session, createdTime: Date.now() }),
72+
);
73+
}
74+
}
75+
76+
return response;
77+
}),
78+
);
79+
1080
const httpLink = new HttpLink({
1181
uri: PUBLIC_GATEWAY_URL,
1282
});
1383

1484
export const makeClient = () => {
1585
return new NextSSRApolloClient({
86+
ssrMode: true,
1687
connectToDevTools: true,
1788
cache: new NextSSRInMemoryCache(),
1889
link: from([
90+
wooSessionSetter,
91+
wooSessionUpdater,
1992
createErrorLink(),
20-
...(typeof window === 'undefined'
21-
? [
22-
new SSRMultipartLink({
23-
stripDefer: true,
24-
}),
25-
httpLink,
26-
]
27-
: [httpLink]),
93+
new SSRMultipartLink({
94+
stripDefer: true,
95+
}),
96+
httpLink,
2897
]),
29-
defaultOptions: {
30-
query: {
31-
fetchPolicy: 'no-cache',
32-
},
33-
},
3498
});
3599
};

0 commit comments

Comments
 (0)