Files
perfil/nuxt4/app/composables/useAuthentik.ts
josedario87 b7285316cf Add session status check button
Add new button to check and display current session status:
- New checkSessionStatus() function in useAuthentik composable
- Displays toast notification with session info
- Shows user name if authenticated or warning if not
- Add UNotifications component to app.vue to render toasts

This allows users to quickly verify their authentication status.
2025-10-13 01:20:45 -06:00

83 lines
2.4 KiB
TypeScript

/**
* Composable para leer información de usuario de Authentik
* Los headers son inyectados por Authentik Proxy Outpost
*/
export const useAuthentik = () => {
// Leer headers en el servidor y almacenarlos en state
const authentikUser = useState('authentikUser', () => {
// Solo en el servidor, leer los headers
if (process.server) {
const headers = useRequestHeaders()
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, el usuario no está autenticado
if (!username) {
return null
}
return {
username,
email,
name,
groups: groups ? groups.split('|') : [],
uid,
// Generar avatar URL usando UI Avatars
avatar: `https://ui-avatars.com/api/?name=${encodeURIComponent(name || username)}&background=random&size=128`
}
}
return null
})
const user = computed(() => authentikUser.value)
const isAuthenticated = computed(() => !!user.value)
const logout = () => {
// Logout completo: invalida la sesión de Authentik completamente
// Esto cierra sesión en todas las aplicaciones
const authentikUrl = useRuntimeConfig().public.authentikUrl || 'https://authentik.nucleoriofrio.com'
navigateTo(`${authentikUrl}/flows/-/default/invalidation/`, { external: true })
}
const goToProfile = () => {
// URL de perfil de Authentik
const authentikUrl = useRuntimeConfig().public.authentikUrl || 'https://authentik.nucleoriofrio.com'
navigateTo(`${authentikUrl}/if/user/`, { external: true, open: { target: '_blank' } })
}
const checkSessionStatus = () => {
const toast = useToast()
if (isAuthenticated.value && user.value) {
toast.add({
title: 'Sesión Activa',
description: `Conectado como: ${user.value.name || user.value.username}`,
color: 'success',
icon: 'i-heroicons-check-circle',
timeout: 5000
})
} else {
toast.add({
title: 'Sin Sesión',
description: 'No hay sesión activa en este momento',
color: 'warning',
icon: 'i-heroicons-exclamation-triangle',
timeout: 5000
})
}
}
return {
user,
isAuthenticated,
logout,
goToProfile,
checkSessionStatus
}
}