import cookie from "js-cookie";
import { redirect } from "@sveltejs/kit";

export async function load({ fetch }) {
    window.moment.locale("id");

    const response = await fetch(`${import.meta.env.VITE_SERVER_HOST}/users/master`, {
        headers: {
            Authorization: cookie.get("session") || "",
        },
    });

    if (!response.ok) {
        redirect(301, "/login");
    }

    const user = (await response.json())?.data;

    if (!user) {
        redirect(301, "/login");
    }

    return { user };
}
