fix: corregir race condition en favoritos con Supabase INITIAL_SESSION
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user