- Add Nuxt 4 application with Authentik Proxy Outpost integration - Add EditProfileButton component for editing user profile via Authentik API - Add API endpoints for Authentik user management (GET/PATCH) - Configure Gitea Actions workflow for automated deployment - Add monitoring hook for Gitea Actions - Configure environment variables and Docker Compose setup
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
/**
|
|
* Actualiza la información del usuario en Authentik
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const config = useRuntimeConfig()
|
|
const headers = getRequestHeaders(event)
|
|
|
|
// Obtener el username desde los headers de Authentik
|
|
const username = headers['x-authentik-username']
|
|
|
|
if (!username) {
|
|
throw createError({
|
|
statusCode: 401,
|
|
message: 'Usuario no autenticado'
|
|
})
|
|
}
|
|
|
|
// Obtener la URL y token de Authentik desde variables de entorno
|
|
const authentikUrl = config.authentikApiUrl || config.public.authentikUrl
|
|
const authentikToken = config.authentikApiToken
|
|
|
|
if (!authentikToken) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: 'Token de Authentik no configurado'
|
|
})
|
|
}
|
|
|
|
// Leer el body de la petición
|
|
const body = await readBody(event)
|
|
|
|
try {
|
|
// Primero, obtener el ID del usuario
|
|
const usersResponse = await $fetch(`${authentikUrl}/api/v3/core/users/?username=${username}`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${authentikToken}`,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
})
|
|
|
|
const users = usersResponse as any
|
|
|
|
if (!users.results || users.results.length === 0) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
message: 'Usuario no encontrado'
|
|
})
|
|
}
|
|
|
|
const userId = users.results[0].pk
|
|
|
|
// Actualizar el usuario
|
|
const updateResponse = await $fetch(`${authentikUrl}/api/v3/core/users/${userId}/`, {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Authorization': `Bearer ${authentikToken}`,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: {
|
|
name: body.name,
|
|
// Otros campos que se puedan actualizar
|
|
...(body.email && { email: body.email })
|
|
}
|
|
})
|
|
|
|
return updateResponse
|
|
} catch (error: any) {
|
|
console.error('Error al actualizar usuario en Authentik:', error)
|
|
throw createError({
|
|
statusCode: error.statusCode || 500,
|
|
message: error.message || 'Error al actualizar información del usuario'
|
|
})
|
|
}
|
|
})
|