removed library remains, add helper usage in hooks
This commit is contained in:
@@ -1,58 +1,11 @@
|
||||
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, 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: [],
|
||||
licence_categories: locals.licence_categories,
|
||||
subscriptions: locals.subscriptions,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user