delete obsolote utils.js

This commit is contained in:
Alex
2024-10-13 13:07:37 +02:00
parent 975e3121a5
commit 20012b729e
4 changed files with 165 additions and 127 deletions

View File

@@ -1,6 +1,58 @@
import { BASE_API_URI } from "$lib/utils/constants";
import { refreshCookie } from "$lib/utils/helpers";
import { redirect } from "@sveltejs/kit";
import { library } from "$lib/stores/library";
/** @type {import('./$types').LayoutServerLoad} */
export async function load({ locals }) {
export async function load({ locals, cookies }) {
const jwt = cookies.get("jwt");
try {
// Fetch 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 redirect(302, "/");
}
// 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");
refreshCookie(newToken, null);
return {
user: locals.user,
licence_categories: licence_categoriesData.licence_categories,
subscriptions: subscriptionsData.subscriptions,
};
} catch (error) {
console.error("Error fetching data:", error);
// In case of any error, clear the JWT cookie
cookies.delete("jwt", { path: "/" });
//throw redirect(302, "/");
}
return {
user: locals.user,
licence_categories: [],
subscriptions: [],
};
}