del: about layout

This commit is contained in:
Alex
2024-10-13 13:39:06 +02:00
parent ab8d143aeb
commit 8b3e8a0579
2 changed files with 0 additions and 71 deletions

View File

@@ -1,5 +0,0 @@
/** @type {import('./$types').LayoutLoad} */
export async function load({ fetch, url, data }) {
const { user, subscriptions, licence_categories } = data;
return { fetch, url: url.pathname, user, subscriptions, licence_categories };
}

View File

@@ -1,66 +0,0 @@
import { BASE_API_URI } from "$lib/utils/constants";
/** @type {import('./$types').LayoutServerLoad} */
export async function load({ cookies, fetch, locals }) {
const jwt = cookies.get("jwt");
try {
// Fetch user data, subscriptions, and licence categories in parallel
const [subscriptionsResponse, licenceCategoriesResponse] =
await Promise.all([
fetch(`${BASE_API_URI}/backend/membership/subscriptions`, {
credentials: "include",
headers: { Cookie: `jwt=${jwt}` },
}),
fetch(`${BASE_API_URI}/backend/licence/categories`, {
credentials: "include",
headers: { Cookie: `jwt=${jwt}` },
}),
]);
// Check if any of the responses are not ok
if (!subscriptionsResponse.ok || !licenceCategoriesResponse.ok) {
cookies.delete("jwt", { path: "/" });
throw new Error("One or more API requests failed");
}
// Parse the JSON responses
const [subscriptionsData, licence_categoriesData] = await Promise.all([
subscriptionsResponse.json(),
licenceCategoriesResponse.json(),
]);
// Check if the server sent a new token
const newToken =
subscriptionsResponse.headers.get("Set-Cookie") == null
? licenceCategoriesResponse.headers.get("Set-Cookie")
: subscriptionsResponse.headers.get("Set-Cookie");
if (newToken) {
const match = newToken.match(/jwt=([^;]+)/);
if (match) {
cookies.set("jwt", match[1], {
path: "/",
httpOnly: true,
secure: process.env.NODE_ENV === "production", // Secure in production
sameSite: "lax",
maxAge: 5 * 24 * 60 * 60, // 5 days in seconds
});
}
}
console.dir(subscriptionsData);
console.dir(licence_categoriesData);
return {
user: locals.user,
subscriptions: subscriptionsData.subscriptions,
licence_categories: licence_categoriesData.licence_categories,
};
} catch (error) {
console.error("Error fetching data:", error);
// In case of any error, clear the JWT cookie
cookies.delete("jwt", { path: "/" });
return {
user: locals.user,
subscriptions: null,
licence_categories: null,
};
}
}