frontend:refactor layout.server
This commit is contained in:
@@ -1,28 +1,28 @@
|
|||||||
import { BASE_API_URI } from "$lib/utils/constants";
|
import { BASE_API_URI } from '$lib/utils/constants';
|
||||||
import { redirect } from "@sveltejs/kit";
|
import { redirect } from '@sveltejs/kit';
|
||||||
import { userDatesFromRFC3339, refreshCookie } from "$lib/utils/helpers";
|
import { userDatesFromRFC3339, refreshCookie } from '$lib/utils/helpers';
|
||||||
|
|
||||||
/** @type {import('./$types').LayoutServerLoad} */
|
/** @type {import('./$types').LayoutServerLoad} */
|
||||||
export async function load({ cookies, fetch, locals }) {
|
export async function load({ cookies, fetch, locals }) {
|
||||||
const jwt = cookies.get("jwt");
|
const jwt = cookies.get('jwt');
|
||||||
try {
|
try {
|
||||||
// Fetch user data, subscriptions, and licence categories in parallel
|
// Fetch user data, subscriptions, and licence categories in parallel
|
||||||
const response = await fetch(`${BASE_API_URI}/backend/users/all`, {
|
const response = await fetch(`${BASE_API_URI}/backend/users/all`, {
|
||||||
credentials: "include",
|
credentials: 'include',
|
||||||
headers: {
|
headers: {
|
||||||
Cookie: `jwt=${jwt}`,
|
Cookie: `jwt=${jwt}`
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// Clear the invalid JWT cookie
|
// Clear the invalid JWT cookie
|
||||||
cookies.delete("jwt", { path: "/" });
|
cookies.delete('jwt', { path: '/' });
|
||||||
throw redirect(302, "/auth/login?next=/");
|
throw redirect(302, '/auth/login?next=/');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
// 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');
|
||||||
refreshCookie(newToken, null);
|
refreshCookie(newToken, cookies);
|
||||||
|
|
||||||
/** @type {App.Locals['users']}*/
|
/** @type {App.Locals['users']}*/
|
||||||
const users = data.users;
|
const users = data.users;
|
||||||
@@ -36,13 +36,13 @@ export async function load({ cookies, fetch, locals }) {
|
|||||||
subscriptions: locals.subscriptions,
|
subscriptions: locals.subscriptions,
|
||||||
licence_categories: locals.licence_categories,
|
licence_categories: locals.licence_categories,
|
||||||
users: locals.users,
|
users: locals.users,
|
||||||
user: locals.user,
|
user: locals.user
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching data:", error);
|
console.error('Error fetching data:', error);
|
||||||
// In case of any error, clear the JWT cookie
|
// In case of any error, clear the JWT cookie
|
||||||
cookies.delete("jwt", { path: "/" });
|
cookies.delete('jwt', { path: '/' });
|
||||||
|
|
||||||
throw redirect(302, "/auth/login?next=/");
|
throw redirect(302, '/auth/login?next=/');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user