2025-05-23 21:32:20 +02:00
|
|
|
import { appendResponseHeader } from "h3";
|
|
|
|
import type { H3Event } from "h3";
|
|
|
|
|
|
|
|
const fetchWithCookie = async (url: string, event?: H3Event) => {
|
|
|
|
if (!event) {
|
|
|
|
return $fetch(url);
|
2025-03-07 16:45:40 +01:00
|
|
|
}
|
2025-05-23 21:32:20 +02:00
|
|
|
|
|
|
|
const cookie = useRequestHeader("cookie");
|
|
|
|
const res = await $fetch.raw(url, {
|
|
|
|
headers: cookie ? { cookie } : undefined
|
|
|
|
});
|
|
|
|
for (const cookie of res.headers.getSetCookie()) {
|
|
|
|
appendResponseHeader(event, "set-cookie", cookie);
|
|
|
|
}
|
|
|
|
return res._data;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useAccountSession = () => {
|
|
|
|
const event = useRequestEvent();
|
|
|
|
return useAsyncData(
|
|
|
|
"session",
|
|
|
|
async () => await fetchWithCookie("/api/auth/session", event),
|
|
|
|
{
|
|
|
|
transform: (input) => input === undefined ? false as any as null: input,
|
|
|
|
getCachedData(key, nuxtApp, context) {
|
|
|
|
if (context.cause === "refresh:manual")
|
|
|
|
return undefined
|
|
|
|
return nuxtApp.payload.data[key];
|
|
|
|
},
|
|
|
|
dedupe: "defer",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|