import { redirect } from '@sveltejs/kit';
import type { LayoutServerLoad } from './$types.js';
import { CookieAuthentication } from '$lib/blog/auth/CookieAuthentication.js';

export const load: LayoutServerLoad = ({ cookies, route }) => {
    const auth = new CookieAuthentication(cookies);
    const isAuthd = auth.isAuthdAsAdmin;

    if (route.id === '/admin/login' && isAuthd) {
        return redirect(307, '/admin');
    } else if (!isAuthd && route.id !== '/admin/login') {
        return redirect(307, '/admin/login');
    }

    return {};
};
