Files
sgse-app/apps/web/src/lib/hooks/useConvexWithAuth.ts
deyvisonwanderley 5ef6ef8550 feat: enhance SLA management and authentication handling
- Updated the useConvexWithAuth hook to improve token management and logging for better debugging.
- Integrated automatic authentication handling in the central-chamados route, ensuring seamless user experience.
- Added a new mutation for migrating old SLA configurations to include a priority field, enhancing data consistency.
- Improved the display of SLA configurations in the UI, including detailed views and migration feedback for better user interaction.
- Refactored ticket loading logic to enrich ticket data with responsible user names, improving clarity in ticket management.
2025-11-17 08:44:18 -03:00

56 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Hook personalizado que garante autenticação no Convex
*
* Este hook substitui useConvexClient e garante que o token seja sempre passado
*
* NOTA: Este hook deve ser usado dentro de componentes Svelte com $effect
*/
import { useConvexClient } from "convex-svelte";
import { authStore } from "$lib/stores/auth.svelte";
interface ConvexClientWithAuth {
setAuth?: (token: string) => void;
clearAuth?: () => void;
}
/**
* Hook que retorna cliente Convex com autenticação configurada automaticamente
*
* IMPORTANTE: Use $effect() no componente para chamar esta função:
* ```svelte
* $effect(() => {
* useConvexWithAuth();
* });
* ```
*/
export function useConvexWithAuth() {
const client = useConvexClient();
const token = authStore.token;
const clientWithAuth = client as ConvexClientWithAuth;
// Configurar token se disponível
if (clientWithAuth && token) {
try {
// Tentar setAuth se disponível
if (typeof clientWithAuth.setAuth === "function") {
clientWithAuth.setAuth(token);
if (import.meta.env.DEV) {
console.log("✅ [useConvexWithAuth] Token configurado via setAuth:", token.substring(0, 20) + "...");
}
} else {
// Se setAuth não estiver disponível, o token deve ser passado via createSvelteAuthClient
if (import.meta.env.DEV) {
console.log(" [useConvexWithAuth] Token disponível, autenticação gerenciada por createSvelteAuthClient");
}
}
} catch (e) {
console.warn("⚠️ [useConvexWithAuth] Erro ao configurar token:", e);
}
} else if (!token && import.meta.env.DEV) {
console.warn("⚠️ [useConvexWithAuth] Token não disponível");
}
return client;
}