import { PRIVATE_ADMIN_AUTH_TOKEN } from '$env/static/private';
import { redirect } from '@sveltejs/kit';
import type { Actions } from './$types.js';
import { CookieAuthentication } from '$lib/blog/auth/CookieAuthentication.js';

export const actions = {
    default: async ({ cookies, request }) => {
        const formData = await request.formData();
        const token = formData.get('token');

        const isAuthd = PRIVATE_ADMIN_AUTH_TOKEN === token;
        const auth = new CookieAuthentication(cookies);

        auth.setAdminAuthentication(isAuthd);

        if (isAuthd) {
            return redirect(307, '/admin');
        }

        return {
            isAuthd,
        };
    },
} satisfies Actions;
