Files
RepoDructor/server/api/auth/status.get.ts
josedario87 01f7a0fd2a
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 24s
fix: implementar verificación correcta de sesión con Authentik
Problema:
La verificación de sesión usaba HEAD /api/music que no es confiable
porque:
- Los headers de Authentik solo están disponibles en el servidor
- El Service Worker puede servir respuestas cacheadas
- No había headers no-cache para evitar respuestas obsoletas

Solución:
Implementar verificación correcta basada en plantillaNuxtAuthentikProxy:

1. Nuevo endpoint /api/auth/status.get.ts
   - Lee headers de Authentik directamente desde el servidor
   - Headers no-cache para verificación en tiempo real
   - Retorna estado autenticado + info de usuario

2. Actualizar useAuth.ts
   - checkAuth() ahora usa /api/auth/status
   - fetchUserInfo() también usa el nuevo endpoint
   - Lógica simplificada y más confiable

Con esto, la verificación de sesión es precisa y en tiempo real,
consultando directamente los headers de Authentik en el servidor.
2025-10-17 04:09:28 -06:00

44 lines
1.1 KiB
TypeScript

/**
* API endpoint para verificar el estado de autenticación en tiempo real
* Consulta los headers inyectados por Authentik Proxy Outpost
*/
export default defineEventHandler((event) => {
// Establecer headers para prevenir caching
setResponseHeaders(event, {
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
})
// Leer headers de Authentik en tiempo real
const headers = getHeaders(event)
const username = headers['x-authentik-username']
const email = headers['x-authentik-email']
const name = headers['x-authentik-name']
const groups = headers['x-authentik-groups']
const uid = headers['x-authentik-uid']
// Si no hay username, no hay sesión activa en Authentik
if (!username) {
return {
authenticated: false,
user: null,
timestamp: new Date().toISOString()
}
}
// Sesión activa
return {
authenticated: true,
user: {
username,
email,
name,
groups: groups ? groups.split('|') : [],
uid
},
timestamp: new Date().toISOString()
}
})