import { component$, Slot } from "@builder.io/qwik";
import type { DocumentHead, RequestHandler } from "@builder.io/qwik-city";
import axios from "axios";

export const onGet: RequestHandler = async ({ cookie, redirect }) => {
    const jwt = cookie.get('access_token')?.value;
    console.log(jwt + "이것은 auth로 들어온 token");
    if (jwt) {
        try {
            const res = await axios.get('http://localhost:8000/api/user/me', {
                headers: {
                    Authorization: `Bearer ${jwt}`,
                },
            });
            console.log(res.data);
            console.log(encodeURIComponent(res.data.username));
            throw redirect(302, `/${encodeURIComponent(res.data.username)}/`);
        } catch (error : any) {
            console.error(error);
        }
    }
};

export default component$(() => {
    return (
        <>
            <main class="bg-custom-gradient w-full h-full">
                <Slot />
            </main>
        </>
    );
});

export const head: DocumentHead = {
    title: "Auth",
    meta: [
        {
            name: "description",
            content: "Auth page description",
        },
    ],
};