fix: corregir race condition en favoritos con Supabase INITIAL_SESSION

This commit is contained in:
2026-03-04 16:03:11 -05:00
parent c178523c7e
commit ef5955cea2
3 changed files with 41 additions and 17 deletions

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted } from 'vue'
import { computed, onMounted, onUnmounted, watch } from 'vue'
import { RouterView, useRoute } from "vue-router";
import { useI18n } from 'vue-i18n'
import MainLayout from "./components/layouts/MainLayout.vue";
@ -155,15 +155,24 @@ onMounted(() => {
event_name: 'app_open',
properties: { language: locale.value }
})
// Load favorites if the user is already logged in
if (authStore.isAuthenticated) {
favoritesStore.loadFavorites()
}
document.addEventListener('visibilitychange', handleVisibilityChange)
window.addEventListener('pageshow', handlePageShow)
window.addEventListener('focus', handleWindowFocus)
})
// Cargar favoritos cuando Supabase confirme la sesión (puede ser después de onMounted)
// immediate:true cubre el caso donde la sesión ya está lista al arrancar
const stopFavWatcher = watch(
() => authStore.isAuthenticated,
(authenticated) => {
if (authenticated && favoritesStore.favorites.length === 0 && !favoritesStore.isLoading) {
favoritesStore.loadFavorites()
stopFavWatcher() // Solo necesitamos cargarlo una vez al inicio
}
},
{ immediate: true }
)
onUnmounted(() => {
document.removeEventListener('visibilitychange', handleVisibilityChange)
window.removeEventListener('pageshow', handlePageShow)