feat: add credentials:include to all API calls and withCredentials to socket

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Aaron Wood 2026-04-11 00:35:01 -04:00
parent a37600fdfa
commit 11714ecbe4
2 changed files with 34 additions and 11 deletions

View file

@ -13,6 +13,7 @@ const BASE = "/api";
async function request<T>(path: string, options?: RequestInit): Promise<T> { async function request<T>(path: string, options?: RequestInit): Promise<T> {
const res = await fetch(`${BASE}${path}`, { const res = await fetch(`${BASE}${path}`, {
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
credentials: "include",
...options, ...options,
}); });
if (!res.ok) { if (!res.ok) {
@ -23,6 +24,27 @@ async function request<T>(path: string, options?: RequestInit): Promise<T> {
return res.json(); return res.json();
} }
// Auth
export interface AuthUser {
userId: number;
email: string;
username: string;
}
export const getMe = () => request<AuthUser>("/auth/me");
export const login = (email: string, password: string) =>
request<AuthUser>("/auth/login", {
method: "POST",
body: JSON.stringify({ email, password }),
});
export const register = (email: string, username: string, password: string) =>
request<AuthUser>("/auth/register", {
method: "POST",
body: JSON.stringify({ email, username, password }),
});
export const logout = () =>
request<{ ok: boolean }>("/auth/logout", { method: "POST" });
// Campaigns // Campaigns
export const getCampaigns = () => request<Campaign[]>("/campaigns"); export const getCampaigns = () => request<Campaign[]>("/campaigns");
export const createCampaign = (name: string) => export const createCampaign = (name: string) =>
@ -32,6 +54,14 @@ export const createCampaign = (name: string) =>
}); });
export const deleteCampaign = (id: number) => export const deleteCampaign = (id: number) =>
request<void>(`/campaigns/${id}`, { method: "DELETE" }); request<void>(`/campaigns/${id}`, { method: "DELETE" });
export const getMyCampaignRole = (campaignId: number) =>
request<{ role: "dm" | "player" }>(`/campaigns/${campaignId}/my-role`);
export const generateInvite = (campaignId: number) =>
request<{ url: string }>(`/campaigns/${campaignId}/invite`, { method: "POST" });
export const joinCampaign = (token: string) =>
request<{ campaignId: number; role: string }>(`/campaigns/join/${token}`, {
method: "POST",
});
// Characters // Characters
export const getCharacters = (campaignId: number) => export const getCharacters = (campaignId: number) =>
@ -53,11 +83,7 @@ export const deleteCharacter = (id: number) =>
request<void>(`/characters/${id}`, { method: "DELETE" }); request<void>(`/characters/${id}`, { method: "DELETE" });
// Stats // Stats
export const updateStat = ( export const updateStat = (characterId: number, statName: string, value: number) =>
characterId: number,
statName: string,
value: number,
) =>
request<{ characterId: number; statName: string; value: number }>( request<{ characterId: number; statName: string; value: number }>(
`/characters/${characterId}/stats/${statName}`, `/characters/${characterId}/stats/${statName}`,
{ method: "PATCH", body: JSON.stringify({ value }) }, { method: "PATCH", body: JSON.stringify({ value }) },
@ -80,9 +106,7 @@ export const addGear = (
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
export const removeGear = (characterId: number, gearId: number) => export const removeGear = (characterId: number, gearId: number) =>
request<void>(`/characters/${characterId}/gear/${gearId}`, { request<void>(`/characters/${characterId}/gear/${gearId}`, { method: "DELETE" });
method: "DELETE",
});
// Talents // Talents
export const addTalent = ( export const addTalent = (
@ -99,9 +123,7 @@ export const addTalent = (
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
export const removeTalent = (characterId: number, talentId: number) => export const removeTalent = (characterId: number, talentId: number) =>
request<void>(`/characters/${characterId}/talents/${talentId}`, { request<void>(`/characters/${characterId}/talents/${talentId}`, { method: "DELETE" });
method: "DELETE",
});
// Game Items // Game Items
export const getGameItems = () => request<GameItem[]>("/game-items"); export const getGameItems = () => request<GameItem[]>("/game-items");

View file

@ -3,6 +3,7 @@ import { io } from "socket.io-client";
const socket = io("/", { const socket = io("/", {
autoConnect: true, autoConnect: true,
reconnection: true, reconnection: true,
withCredentials: true,
}); });
export default socket; export default socket;