This commit is contained in:
Leonardo
2026-03-06 06:37:13 -03:00
parent d58dc21297
commit f733db8436
146 changed files with 43436 additions and 12779 deletions
+111 -21
View File
@@ -1,38 +1,128 @@
<script setup>
import { onMounted } from 'vue'
import { onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { supabase } from '@/lib/supabase/client'
import { useTenantStore } from '@/stores/tenantStore'
import { useEntitlementsStore } from '@/stores/entitlementsStore'
const route = useRoute()
const tenantStore = useTenantStore()
const entStore = useEntitlementsStore()
onMounted(async () => {
// 1) carrega sessão + tenant ativo (do seu fluxo atual)
await tenantStore.loadSessionAndTenant()
function isTenantArea (path = '') {
return path.startsWith('/admin') || path.startsWith('/therapist')
}
// 2) carrega permissões do tenant ativo (se existir)
if (tenantStore.activeTenantId) {
await entStore.loadForTenant(tenantStore.activeTenantId)
function isPortalArea (path = '') {
return path.startsWith('/portal')
}
function isSaasArea (path = '') {
return path.startsWith('/saas')
}
async function debugSnapshot (label = 'snapshot') {
console.group(`🧭 [APP DEBUG] ${label}`)
try {
// 0) rota + meta
console.log('route.fullPath:', route.fullPath)
console.log('route.path:', route.path)
console.log('route.name:', route.name)
console.log('route.meta:', route.meta)
// 1) storage
console.groupCollapsed('📦 Storage')
console.log('localStorage.tenant_id:', localStorage.getItem('tenant_id'))
console.log('localStorage.currentTenantId:', localStorage.getItem('currentTenantId'))
console.log('localStorage.tenant:', localStorage.getItem('tenant'))
console.log('sessionStorage.redirect_after_login:', sessionStorage.getItem('redirect_after_login'))
console.log('sessionStorage.intended_area:', sessionStorage.getItem('intended_area'))
console.groupEnd()
// 2) sessão auth (fonte real)
const { data: authData, error: authErr } = await supabase.auth.getUser()
if (authErr) console.warn('[auth.getUser] error:', authErr)
const user = authData?.user || null
console.log('auth.user:', user ? { id: user.id, email: user.email } : null)
// 3) profiles.role (identidade global)
let profileRole = null
if (user?.id) {
const { data: profile, error: pErr } = await supabase
.from('profiles')
.select('role')
.eq('id', user.id)
.single()
if (pErr) console.warn('[profiles] error:', pErr)
profileRole = profile?.role || null
}
console.log('profiles.role (global):', profileRole)
// 4) memberships via RPC (fonte de verdade do tenantStore)
let rpcTenants = null
if (user?.id) {
const { data: rpcData, error: rpcErr } = await supabase.rpc('my_tenants')
if (rpcErr) console.warn('[rpc my_tenants] error:', rpcErr)
rpcTenants = rpcData ?? null
}
console.log('rpc.my_tenants():', rpcTenants)
// 5) stores (sempre logar)
console.groupCollapsed('🏪 Stores (before optional loads)')
console.log('tenantStore.activeTenantId:', tenantStore.activeTenantId)
console.log('tenantStore.activeRole:', tenantStore.activeRole)
console.log('tenantStore.memberships:', tenantStore.memberships)
console.log('entStore.loaded:', entStore.loaded)
console.log('entStore.tenantId:', entStore.activeTenantId || entStore.tenantId)
console.groupEnd()
// 6) IMPORTANTÍSSIMO: não carregar tenant fora da área tenant
const path = route.path || ''
if (isTenantArea(path)) {
console.log('✅ Tenant area detected → will loadSessionAndTenant + entitlements')
await tenantStore.loadSessionAndTenant()
if (tenantStore.activeTenantId) {
await entStore.loadForTenant(tenantStore.activeTenantId)
}
console.groupCollapsed('🏪 Stores (after tenant loads)')
console.log('tenantStore.activeTenantId:', tenantStore.activeTenantId)
console.log('tenantStore.activeRole:', tenantStore.activeRole)
console.log('tenantStore.memberships:', tenantStore.memberships)
console.log("entStore.can('online_scheduling.manage'):", entStore.can?.('online_scheduling.manage'))
console.groupEnd()
} else if (isPortalArea(path)) {
console.log('🟣 Portal area detected → SKIP tenantStore.loadSessionAndTenant()')
} else if (isSaasArea(path)) {
console.log('🟠 SaaS area detected → SKIP tenantStore.loadSessionAndTenant()')
} else {
console.log('⚪ Other/public area detected → SKIP tenantStore.loadSessionAndTenant()')
}
} catch (e) {
console.error('[APP DEBUG] snapshot error:', e)
} finally {
console.groupEnd()
}
}
// 3) debug: localStorage com rótulos
console.groupCollapsed('[Debug] Tenant localStorage')
console.log('tenant_id:', localStorage.getItem('tenant_id'))
console.log('currentTenantId:', localStorage.getItem('currentTenantId'))
console.log('tenant:', localStorage.getItem('tenant'))
console.groupEnd()
onMounted(async () => {
// 🔥 PRIMEIRO LOG — TENANT ID BRUTO (mantive sua ideia)
console.log('[SEU_TENANT_ID]', localStorage.getItem('tenant_id'))
// 4) debug: stores
console.groupCollapsed('[Debug] Tenant stores')
console.log('route:', route.fullPath)
console.log('activeTenantId:', tenantStore.activeTenantId)
console.log('activeRole:', tenantStore.activeRole)
console.log("can('online_scheduling.manage'):", entStore.can('online_scheduling.manage'))
console.groupEnd()
// snapshot inicial
await debugSnapshot('mounted')
})
// snapshot a cada navegação (isso é o que vai te salvar)
watch(
() => route.fullPath,
async (to, from) => {
await debugSnapshot(`route change: ${from} -> ${to}`)
}
)
</script>
<template>