removed library remains, add helper usage in hooks

This commit is contained in:
Alex
2024-10-13 13:41:36 +02:00
parent d3365ae065
commit 58daf7bf30
4 changed files with 29 additions and 99 deletions

View File

@@ -1,4 +1,5 @@
import { BASE_API_URI } from "$lib/utils/constants.js"; import { BASE_API_URI } from "$lib/utils/constants.js";
import { refreshCookie, userDatesFromRFC3339 } from "$lib/utils/helpers";
/** @type {import('@sveltejs/kit').Handle} */ /** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) { export async function handle({ event, resolve }) {
@@ -30,52 +31,29 @@ export async function handle({ event, resolve }) {
// Check if the server sent a new token // Check if the server sent a new token
const newToken = response.headers.get("Set-Cookie"); const newToken = response.headers.get("Set-Cookie");
if (newToken) { refreshCookie(newToken, event);
const match = newToken.match(/jwt=([^;]+)/);
if (match) {
event.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
});
}
}
event.locals.subscriptions = data.subscriptions; userDatesFromRFC3339(data.user);
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}` },
}),
]);
const [subscriptionsData, licence_categoriesData] = await Promise.all([
subscriptionsResponse.json(),
licenceCategoriesResponse.json(),
]);
event.locals.user = data.user; event.locals.user = data.user;
event.locals.licence_categories = data.licence_categories; event.locals.subscriptions = subscriptionsData.subscriptions;
console.dir(event.locals.user); event.locals.licence_categories = licence_categoriesData.licence_categories;
if (event.locals.user.date_of_birth) { // console.log("hooks.server: Printing locals:");
event.locals.user.date_of_birth = // console.dir(event.locals);
event.locals.user.date_of_birth.split("T")[0];
}
if (event.locals.user.membership) {
if (event.locals.user.membership.start_date) {
event.locals.user.membership.start_date =
event.locals.user.membership.start_date.split("T")[0];
}
if (event.locals.user.membership.end_date) {
event.locals.user.membership.end_date =
event.locals.user.membership.end_date.split("T")[0];
}
}
if (event.locals.user.licence?.issued_date) {
event.locals.user.licence.issued_date =
event.locals.user.licence.issued_date.split("T")[0];
}
if (event.locals.user.licence?.expiration_date) {
event.locals.user.licence.expiration_date =
event.locals.user.licence.expiration_date.split("T")[0];
}
if (
event.locals.user.bank_account &&
event.locals.user.bank_account.mandate_date_signed
) {
event.locals.user.bank_account.mandate_date_signed =
event.locals.user.bank_account.mandate_date_signed.split("T")[0];
}
// load page as normal // load page as normal
return await resolve(event); return await resolve(event);

View File

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

View File

@@ -1,58 +1,11 @@
import { BASE_API_URI } from "$lib/utils/constants"; import { BASE_API_URI } from "$lib/utils/constants";
import { refreshCookie } from "$lib/utils/helpers"; import { refreshCookie } from "$lib/utils/helpers";
import { redirect } from "@sveltejs/kit"; import { redirect } from "@sveltejs/kit";
import { library } from "$lib/stores/library";
/** @type {import('./$types').LayoutServerLoad} */ /** @type {import('./$types').LayoutServerLoad} */
export async function load({ locals, cookies }) { 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 { return {
user: locals.user, user: locals.user,
licence_categories: licence_categoriesData.licence_categories, licence_categories: locals.licence_categories,
subscriptions: subscriptionsData.subscriptions, subscriptions: locals.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: [],
}; };
} }

View File

@@ -4,14 +4,11 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
import { page } from "$app/stores"; import { page } from "$app/stores";
import { t } from "svelte-i18n"; import { t } from "svelte-i18n";
import { library } from "$lib/stores/library";
/** @type {import('./$types').ActionData} */ /** @type {import('./$types').ActionData} */
export let form; export let form;
$: user = $page.data.user; $: ({ user, licence_categories, subscriptions } = $page.data);
$: ({ licence_categories, subscriptions } = $library);
let showModal = false; let showModal = false;