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

@ -12,7 +12,7 @@
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { ref, computed, watch } from 'vue'
import { useFavoritesStore } from '@/stores/favorites'
import { useAuthStore } from '@/stores/auth'
@ -31,16 +31,22 @@ const isFavorited = computed(() => {
return favoritesStore.isFavorite(props.itemType, props.itemId)
})
onMounted(() => {
// Load favorites if authenticated and not loaded yet
if (authStore.isAuthenticated && favoritesStore.favorites.length === 0) {
favoritesStore.loadFavorites()
}
})
// Observamos isAuthenticated con immediate:true para cubrir 2 casos:
// 1. El usuario ya estaba autenticado cuando el componente montó.
// 2. Supabase completó INITIAL_SESSION DESPUÉS de que el componente montó
// (race condition común al navegar directamente a una URL).
watch(
() => authStore.isAuthenticated,
(authenticated) => {
if (authenticated && favoritesStore.favorites.length === 0 && !favoritesStore.isLoading) {
favoritesStore.loadFavorites()
}
},
{ immediate: true }
)
async function handleToggle() {
if (!authStore.isAuthenticated) {
// Optionally redirect to login or show message
alert('Debes iniciar sesión para agregar favoritos')
return
}