- Introduced "Minhas Intenções" tab visible only to logged-in users. - Integrated MyPurchaseIntentionsPanel and useMyPurchaseIntentions hook. - Added tab badge showing count of pending intentions. - Extended session API to include user’s main character for downstream features.
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
export interface SessionUser {
|
|
userId: number;
|
|
username: string;
|
|
email: string;
|
|
characterName: string | null;
|
|
}
|
|
|
|
interface SessionResponse {
|
|
success: boolean;
|
|
authenticated: boolean;
|
|
user?: SessionUser;
|
|
}
|
|
|
|
/**
|
|
* Hook para gerenciar sessão do usuário com React Query
|
|
* Atualiza automaticamente a cada 30 segundos
|
|
*/
|
|
export function useSession() {
|
|
const { data, isLoading, refetch } = useQuery<SessionResponse>({
|
|
queryKey: ["session"],
|
|
queryFn: async () => {
|
|
const response = await fetch("/api/auth/session", {
|
|
method: "GET",
|
|
credentials: "include",
|
|
});
|
|
return response.json();
|
|
},
|
|
staleTime: 1000 * 30, // 30 segundos
|
|
refetchInterval: 1000 * 30, // Refetch a cada 30 segundos
|
|
refetchOnWindowFocus: true,
|
|
refetchOnReconnect: true,
|
|
});
|
|
|
|
const logout = async () => {
|
|
try {
|
|
await fetch("/api/auth/session", {
|
|
method: "DELETE",
|
|
credentials: "include",
|
|
});
|
|
// Invalidar cache e recarregar
|
|
window.location.href = "/";
|
|
} catch (error) {
|
|
console.error("Erro ao fazer logout:", error);
|
|
}
|
|
};
|
|
|
|
return {
|
|
user: data?.user || null,
|
|
isLoading,
|
|
isAuthenticated: data?.authenticated || false,
|
|
logout,
|
|
refreshSession: refetch,
|
|
};
|
|
}
|
|
|