Files
MediaManager/web/src/routes/dashboard/+layout.ts
2025-06-22 18:05:21 +02:00

28 lines
710 B
TypeScript

import {env} from '$env/dynamic/public';
import type {LayoutLoad} from './$types';
import {redirect} from '@sveltejs/kit';
import {base} from '$app/paths';
import {browser} from '$app/environment';
import {goto} from '$app/navigation';
const apiUrl = env.PUBLIC_API_URL;
export const load: LayoutLoad = async ({fetch}) => {
const response = await fetch(apiUrl + '/users/me', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include'
});
if (!response.ok) {
console.log('unauthorized, redirecting to login');
if (browser) {
await goto(base + '/login');
} else {
throw redirect(303, base + '/login');
}
}
return {user: await response.json()};
};