Fix TypeScript errors in useAuthentik composable
- Add AuthentikUser and AuthStatusResponse interfaces - Fix toast color from 'gray' to 'neutral' (valid color) - Remove invalid 'timeout' property from toast.add() - Fix toast action from 'click' to 'onClick' - Add proper type annotations for error handling - Replace process.server with import.meta.server - Make email, name, uid optional in AuthentikUser interface
This commit is contained in:
@@ -2,11 +2,29 @@
|
|||||||
* Composable para leer información de usuario de Authentik
|
* Composable para leer información de usuario de Authentik
|
||||||
* Los headers son inyectados por Authentik Proxy Outpost
|
* Los headers son inyectados por Authentik Proxy Outpost
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
interface AuthentikUser {
|
||||||
|
username: string
|
||||||
|
email: string | undefined
|
||||||
|
name: string | undefined
|
||||||
|
groups: string[]
|
||||||
|
uid: string | undefined
|
||||||
|
avatar: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AuthStatusResponse {
|
||||||
|
authenticated: boolean
|
||||||
|
user?: {
|
||||||
|
username: string
|
||||||
|
name?: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const useAuthentik = () => {
|
export const useAuthentik = () => {
|
||||||
// Leer headers en el servidor y almacenarlos en state
|
// Leer headers en el servidor y almacenarlos en state
|
||||||
const authentikUser = useState('authentikUser', () => {
|
const authentikUser = useState<AuthentikUser | null>('authentikUser', () => {
|
||||||
// Solo en el servidor, leer los headers
|
// Solo en el servidor, leer los headers
|
||||||
if (process.server) {
|
if (import.meta.server) {
|
||||||
const headers = useRequestHeaders()
|
const headers = useRequestHeaders()
|
||||||
|
|
||||||
const username = headers['x-authentik-username']
|
const username = headers['x-authentik-username']
|
||||||
@@ -58,9 +76,8 @@ export const useAuthentik = () => {
|
|||||||
toast.add({
|
toast.add({
|
||||||
title: 'Modo Offline',
|
title: 'Modo Offline',
|
||||||
description: 'No se puede validar sesión sin conexión',
|
description: 'No se puede validar sesión sin conexión',
|
||||||
color: 'gray',
|
color: 'neutral',
|
||||||
icon: 'i-heroicons-wifi',
|
icon: 'i-heroicons-wifi'
|
||||||
timeout: 5000
|
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -70,13 +87,12 @@ export const useAuthentik = () => {
|
|||||||
title: 'Verificando sesión...',
|
title: 'Verificando sesión...',
|
||||||
description: 'Consultando estado en Authentik',
|
description: 'Consultando estado en Authentik',
|
||||||
color: 'info',
|
color: 'info',
|
||||||
icon: 'i-heroicons-arrow-path',
|
icon: 'i-heroicons-arrow-path'
|
||||||
timeout: 2000
|
|
||||||
})
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Consultar el endpoint de API que verifica contra Authentik
|
// Consultar el endpoint de API que verifica contra Authentik
|
||||||
const response = await $fetch('/api/auth/status')
|
const response = await $fetch<AuthStatusResponse>('/api/auth/status')
|
||||||
|
|
||||||
if (response.authenticated && response.user) {
|
if (response.authenticated && response.user) {
|
||||||
// Sesión activa en Authentik
|
// Sesión activa en Authentik
|
||||||
@@ -84,8 +100,7 @@ export const useAuthentik = () => {
|
|||||||
title: 'Sesión Activa',
|
title: 'Sesión Activa',
|
||||||
description: `Conectado como: ${response.user.name || response.user.username}`,
|
description: `Conectado como: ${response.user.name || response.user.username}`,
|
||||||
color: 'success',
|
color: 'success',
|
||||||
icon: 'i-heroicons-check-circle',
|
icon: 'i-heroicons-check-circle'
|
||||||
timeout: 5000
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// Sin sesión en Authentik
|
// Sin sesión en Authentik
|
||||||
@@ -94,35 +109,33 @@ export const useAuthentik = () => {
|
|||||||
description: 'No hay sesión activa en Authentik',
|
description: 'No hay sesión activa en Authentik',
|
||||||
color: 'warning',
|
color: 'warning',
|
||||||
icon: 'i-heroicons-exclamation-triangle',
|
icon: 'i-heroicons-exclamation-triangle',
|
||||||
timeout: 10000,
|
|
||||||
actions: [{
|
actions: [{
|
||||||
label: 'Iniciar Sesión',
|
label: 'Iniciar Sesión',
|
||||||
click: () => {
|
onClick: () => {
|
||||||
// Recargar la página forzará a Authentik a redirigir al login
|
// Recargar la página forzará a Authentik a redirigir al login
|
||||||
window.location.reload()
|
window.location.reload()
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error: unknown) {
|
||||||
// Verificar si está offline ahora (pudo desconectarse durante la petición)
|
// Verificar si está offline ahora (pudo desconectarse durante la petición)
|
||||||
if (!navigator.onLine) {
|
if (!navigator.onLine) {
|
||||||
toast.add({
|
toast.add({
|
||||||
title: 'Modo Offline',
|
title: 'Modo Offline',
|
||||||
description: 'No se puede validar sesión sin conexión',
|
description: 'No se puede validar sesión sin conexión',
|
||||||
color: 'gray',
|
color: 'neutral',
|
||||||
icon: 'i-heroicons-wifi',
|
icon: 'i-heroicons-wifi'
|
||||||
timeout: 5000
|
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si el error es por redirect de Authentik (CORS/fetch error), significa que no hay sesión
|
// Si el error es por redirect de Authentik (CORS/fetch error), significa que no hay sesión
|
||||||
// Authentik redirige a login cuando no hay sesión válida, causando error CORS en fetch
|
// Authentik redirige a login cuando no hay sesión válida, causando error CORS en fetch
|
||||||
const errorMessage = error?.message || error?.toString() || ''
|
const errorMessage = (error as Error)?.message || String(error)
|
||||||
const isCorsOrRedirectError = errorMessage.includes('Failed to fetch') ||
|
const isCorsOrRedirectError = errorMessage.includes('Failed to fetch') ||
|
||||||
errorMessage.includes('CORS') ||
|
errorMessage.includes('CORS') ||
|
||||||
error?.statusCode === 302
|
(error as any)?.statusCode === 302
|
||||||
|
|
||||||
if (isCorsOrRedirectError) {
|
if (isCorsOrRedirectError) {
|
||||||
// Interpretar como sesión expirada/inválida
|
// Interpretar como sesión expirada/inválida
|
||||||
@@ -131,10 +144,9 @@ export const useAuthentik = () => {
|
|||||||
description: 'No hay sesión activa en Authentik',
|
description: 'No hay sesión activa en Authentik',
|
||||||
color: 'warning',
|
color: 'warning',
|
||||||
icon: 'i-heroicons-exclamation-triangle',
|
icon: 'i-heroicons-exclamation-triangle',
|
||||||
timeout: 10000,
|
|
||||||
actions: [{
|
actions: [{
|
||||||
label: 'Iniciar Sesión',
|
label: 'Iniciar Sesión',
|
||||||
click: () => {
|
onClick: () => {
|
||||||
// Recargar la página forzará a Authentik a redirigir al login
|
// Recargar la página forzará a Authentik a redirigir al login
|
||||||
window.location.reload()
|
window.location.reload()
|
||||||
}
|
}
|
||||||
@@ -146,8 +158,7 @@ export const useAuthentik = () => {
|
|||||||
title: 'Error',
|
title: 'Error',
|
||||||
description: 'No se pudo verificar el estado de la sesión',
|
description: 'No se pudo verificar el estado de la sesión',
|
||||||
color: 'error',
|
color: 'error',
|
||||||
icon: 'i-heroicons-x-circle',
|
icon: 'i-heroicons-x-circle'
|
||||||
timeout: 5000
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
console.error('Error checking session status:', error)
|
console.error('Error checking session status:', error)
|
||||||
|
|||||||
Reference in New Issue
Block a user