feat: integrate Better Auth and enhance authentication flow

- 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.
This commit is contained in:
2025-11-06 09:35:36 -03:00
parent 33f305220b
commit 06f03b53e5
28 changed files with 4109 additions and 436 deletions

View File

@@ -0,0 +1,26 @@
/**
* Hook para garantir que o cliente Convex tenha o token configurado
*
* NOTA: O token é passado automaticamente via monkey patch no +layout.svelte
* Este hook existe apenas para compatibilidade, mas não faz nada agora.
* O token é injetado via headers nas requisições HTTP através do monkey patch.
*/
import { authStore } from "$lib/stores/auth.svelte";
/**
* Configura o token no cliente Convex
*
* IMPORTANTE: O token agora é passado automaticamente via monkey patch global.
* Este hook é mantido para compatibilidade mas não precisa ser chamado.
*
* @param client - Cliente Convex retornado por useConvexClient()
*/
export function setupConvexAuth(client: unknown) {
// Token é passado automaticamente via monkey patch em +layout.svelte
// Não precisamos fazer nada aqui, apenas manter compatibilidade
if (import.meta.env.DEV && client && authStore.token) {
console.log("✅ [setupConvexAuth] Token disponível (gerenciado via monkey patch):", authStore.token.substring(0, 20) + "...");
}
}

View File

@@ -0,0 +1,45 @@
/**
* 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;
}