- Added Better Auth integration to the web application, allowing for dual login support with both custom and Better Auth systems. - Updated authentication client configuration to dynamically set the base URL based on the environment. - Enhanced chat components to utilize user authentication status, improving user experience and security. - Refactored various components to support Better Auth, including error handling and user identity management. - Improved notification handling and user feedback mechanisms during authentication processes.
46 lines
1.2 KiB
TypeScript
46 lines
1.2 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 && typeof clientWithAuth.setAuth === "function" && token) {
|
|
try {
|
|
clientWithAuth.setAuth(token);
|
|
if (import.meta.env.DEV) {
|
|
console.log("✅ [useConvexWithAuth] Token configurado:", token.substring(0, 20) + "...");
|
|
}
|
|
} catch (e) {
|
|
console.warn("⚠️ [useConvexWithAuth] Erro ao configurar token:", e);
|
|
}
|
|
}
|
|
|
|
return client;
|
|
}
|