fix: desregistrar Service Worker al perder autenticación
All checks were successful
build-and-deploy / build (push) Successful in 23s
build-and-deploy / deploy (push) Successful in 3s

- Desregistrar SW automáticamente cuando se detecta pérdida de auth
- Desregistrar SW antes de triggerAuth para evitar conflictos con Authentik
- Deshabilitar polling y listeners de visibility (causan errores de CORS)
- Confiar en detección reactiva de errores del musicStore
- Usar window.location.href en lugar de reload() para forzar navegación
This commit is contained in:
2025-10-12 03:12:03 -06:00
parent fda5b2497a
commit cda4722a27
2 changed files with 50 additions and 13 deletions

View File

@@ -86,24 +86,57 @@ export const useAuth = () => {
}
}
const triggerAuth = () => {
console.log('[Auth] Triggering authentication flow - forcing page reload...')
const triggerAuth = async () => {
console.log('[Auth] Triggering authentication flow...')
// Simplemente recargamos la página completamente
// Si no estamos autenticados, Authentik interceptará y redirigirá al login
// Si ya estamos autenticados, la página se recarga normalmente
window.location.reload()
// Desregistrar el Service Worker temporalmente para evitar conflictos con Authentik
if ('serviceWorker' in navigator) {
try {
const registrations = await navigator.serviceWorker.getRegistrations()
console.log('[Auth] Unregistering Service Workers before auth...')
await Promise.all(registrations.map(reg => reg.unregister()))
} catch (error) {
console.warn('[Auth] Could not unregister SW:', error)
}
}
// Forzar navegación completa a la raíz
// Authentik interceptará y redirigirá al login si no estamos autenticados
console.log('[Auth] Forcing hard navigation to trigger Authentik...')
window.location.href = window.location.origin + '/'
}
const markUnauthenticated = () => {
const markUnauthenticated = async () => {
// Helper para marcar como no autenticado (útil cuando detectamos 401/403)
console.log('[Auth] Marking as unauthenticated')
isAuthenticated.value = false
authChecked.value = true
lastCheckTime.value = Date.now()
// Desregistrar Service Worker para evitar conflictos con Authentik
if ('serviceWorker' in navigator) {
try {
const registrations = await navigator.serviceWorker.getRegistrations()
if (registrations.length > 0) {
console.log('[Auth] Unregistering Service Worker due to auth loss...')
await Promise.all(registrations.map(reg => reg.unregister()))
console.log('[Auth] Service Worker unregistered - page will need reload for re-auth')
}
} catch (error) {
console.warn('[Auth] Could not unregister SW:', error)
}
}
}
const setupVisibilityListener = () => {
// DESHABILITADO: Los listeners causan problemas con Authentik
// Cuando no estamos autenticados, los listeners intentan hacer fetch
// y Authentik redirige, causando errores de CORS
// En su lugar, confiamos en que el usuario haga click en "Reautenticar"
console.log('[Auth] Visibility listeners disabled to avoid conflicts with Authentik')
return
/* CÓDIGO DESHABILITADO
// Re-chequea auth cuando la pestaña vuelve a ser visible
// (útil si el usuario se autentica en otra pestaña)
if (typeof document !== 'undefined' && !visibilityChangeListener) {
@@ -129,6 +162,7 @@ export const useAuth = () => {
}
window.addEventListener('focus', focusListener)
}
*/
}
const cleanupListeners = () => {