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:
45
apps/web/src/lib/hooks/useConvexWithAuth.ts
Normal file
45
apps/web/src/lib/hooks/useConvexWithAuth.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user