Merge remote-tracking branch 'origin' into feat-pedidos

This commit is contained in:
2025-12-11 10:08:12 -03:00
194 changed files with 30374 additions and 10247 deletions

View File

@@ -5,7 +5,10 @@ import { components } from './_generated/api';
import type { DataModel } from './_generated/dataModel';
import { type MutationCtx, type QueryCtx, query } from './_generated/server';
const siteUrl = process.env.SITE_URL!;
// Usar SITE_URL se disponível, caso contrário usar CONVEX_SITE_URL ou um valor padrão
const siteUrl = process.env.SITE_URL || process.env.CONVEX_SITE_URL || 'http://localhost:5173';
console.log('siteUrl:', siteUrl);
// The component client has methods needed for integrating Convex with Better Auth,
// as well as helper methods for general use.
@@ -21,6 +24,7 @@ export const createAuth = (
logger: {
disabled: optionsOnly
},
trustedOrigins: ['https://vite.kilder.dev'],
baseURL: siteUrl,
database: authComponent.adapter(ctx),
// Configure simple, non-verified email/password to get started
@@ -40,32 +44,41 @@ export const createAuth = (
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
const authUser = await authComponent.safeGetAuthUser(ctx);
if (!authUser) {
try {
const authUser = await authComponent.safeGetAuthUser(ctx);
if (!authUser) {
return;
}
const user = await ctx.db
.query('usuarios')
.withIndex('authId', (q) => q.eq('authId', authUser._id))
.unique();
if (!user) {
return;
}
// Buscar foto de perfil e role em paralelo para melhor performance
const [fotoPerfilUrl, role] = await Promise.all([
user.fotoPerfil
? ctx.storage.getUrl(user.fotoPerfil).catch(() => null)
: Promise.resolve(null),
user.roleId
? ctx.db
.query('roles')
.withIndex('by_id', (q) => q.eq('_id', user.roleId))
.unique()
.catch(() => null)
: Promise.resolve(null)
]);
return { ...user, role: role || null, fotoPerfilUrl };
} catch (error) {
// Log do erro mas não falhar completamente - retornar null para permitir retry
console.error('Erro ao buscar usuário atual:', error);
return;
}
const user = await ctx.db
.query('usuarios')
.withIndex('authId', (q) => q.eq('authId', authUser._id))
.unique();
if (!user) {
return;
}
const fotoPerfilUrl = user.fotoPerfil ? await ctx.storage.getUrl(user.fotoPerfil) : null;
if (!user.roleId) {
return { ...user, role: null, fotoPerfilUrl };
}
const role = await ctx.db
.query('roles')
.withIndex('by_id', (q) => q.eq('_id', user.roleId))
.unique();
return { ...user, role, fotoPerfilUrl };
}
});