import { useState, useEffect } from "react"; export interface AuthUser { discordId: string; username: string; avatar: string | null; } interface AuthState { loading: boolean; user: AuthUser | null; } export function useAuth(): AuthState & { logout: () => Promise } { const [state, setState] = useState({ loading: true, user: null }); useEffect(() => { fetch("/auth/me", { credentials: "same-origin" }) .then((r) => r.json()) .then((data: { authenticated: boolean; user?: AuthUser }) => { setState({ loading: false, user: data.authenticated ? data.user! : null, }); }) .catch(() => setState({ loading: false, user: null })); }, []); const logout = async () => { await fetch("/auth/logout", { method: "POST", credentials: "same-origin" }); setState({ loading: false, user: null }); }; return { ...state, logout }; }