| function getCookie(name: string) { | |
| if (typeof document === "undefined") return null; | |
| const match = document.cookie | |
| .split("; ") | |
| .find((row) => row.startsWith(name + "=")); | |
| return match?.split("=")[1] ?? null; | |
| } | |
| export async function authFetch( | |
| url: string, | |
| options?: RequestInit | |
| ) { | |
| const token = getCookie("auth_token"); | |
| return fetch(url, { | |
| ...options, | |
| credentials: "include", | |
| headers: { | |
| "Content-Type": "application/json", | |
| ...(token && { Authorization: `Bearer ${token}` }), | |
| ...options?.headers, | |
| }, | |
| }); | |
| } |