Skip to content

Update apollo.js #1473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 16 additions & 38 deletions plugins/apollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {

import { provideApolloClient } from "@vue/apollo-composable";

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

export default defineNuxtPlugin((nuxtApp) => {
const cookie = useCookie("woo-session", {
maxAge: 1_209_600, // 14 days
sameSite: "lax",
});
const config = useRuntimeConfig();

const httpLink = createHttpLink({
Expand All @@ -20,25 +22,13 @@ export default defineNuxtPlugin((nuxtApp) => {
/**
* If session data exist in local storage, set value as session header.
*/
const sessionData = process.client
? JSON.parse(localStorage.getItem("woo-session"))
: null;

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

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

return forward(operation);
Expand All @@ -55,18 +45,11 @@ export default defineNuxtPlugin((nuxtApp) => {
response: { headers },
} = context;

const session = headers.get("woocommerce-session");
const session = headers.get("woocommerce-session") || cookie.value;

if (session && process.client) {
if (session === "false") {
// Remove session data if session destroyed.
localStorage.removeItem("woo-session");
// Update session new data if changed.
} else if (!localStorage.getItem("woo-session")) {
localStorage.setItem(
"woo-session",
JSON.stringify({ token: session, createdTime: Date.now() })
);
if (process.client && session) {
if (session !== cookie.value) {
cookie.value = session;
}
}
return response;
Expand Down Expand Up @@ -102,11 +85,6 @@ export default defineNuxtPlugin((nuxtApp) => {
provideApolloClient(apolloClient);

nuxtApp.hook("apollo:auth", ({ token }) => {
if (process.client) {
const sessionData = JSON.parse(localStorage.getItem("woo-session"));
if (sessionData && sessionData.token) {
token.value = sessionData.token;
}
}
token.value = cookie.value;
});
});
});