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:
@@ -25,7 +25,8 @@ async function obterMatriculaUsuario(
|
||||
* Usa a mesma lógica do obterPerfil para garantir consistência
|
||||
*/
|
||||
async function getUsuarioAutenticado(ctx: QueryCtx | MutationCtx) {
|
||||
// Tentar autenticação via Better Auth primeiro
|
||||
// FASE 1 IMPLEMENTADA: Usa Custom Auth Provider configurado no convex.config.ts
|
||||
// O provider busca sessão por token específico (seguro) ou Better Auth
|
||||
const identity = await ctx.auth.getUserIdentity();
|
||||
let usuarioAtual = null;
|
||||
|
||||
@@ -36,17 +37,11 @@ async function getUsuarioAutenticado(ctx: QueryCtx | MutationCtx) {
|
||||
.first();
|
||||
}
|
||||
|
||||
// Se não encontrou via Better Auth, tentar via sessão mais recente
|
||||
if (!usuarioAtual) {
|
||||
const sessaoAtiva = await ctx.db
|
||||
.query("sessoes")
|
||||
.filter((q) => q.eq(q.field("ativo"), true))
|
||||
.order("desc")
|
||||
.first();
|
||||
|
||||
if (sessaoAtiva) {
|
||||
usuarioAtual = await ctx.db.get(sessaoAtiva.usuarioId);
|
||||
}
|
||||
if (!usuarioAtual && identity) {
|
||||
console.error("⚠️ [getUsuarioAutenticado] Identity encontrada mas usuário não encontrado no banco:", {
|
||||
email: identity.email,
|
||||
subject: identity.subject
|
||||
});
|
||||
}
|
||||
|
||||
return usuarioAtual;
|
||||
|
||||
Reference in New Issue
Block a user