- 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.
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
/**
|
||
* 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;
|
||
}
|