Fix: Combinar endpoints API en un solo archivo user.ts
PROBLEMA IDENTIFICADO: Nuxt 4 no reconoce archivos con extensiones como .get.ts y .patch.ts en el directorio server/api/. Esto causaba que los endpoints no se compilaran en el build final. SOLUCIÓN: - Eliminados: user.get.ts y user.patch.ts - Creado: user.ts que maneja ambos métodos HTTP (GET y PATCH) - Usa event.method para distinguir entre GET y PATCH - Devuelve 405 para métodos no permitidos Este cambio debería permitir que Nuxt compile correctamente los endpoints de la API de Authentik para edición de perfil.
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
/**
|
||||
* Obtiene la información del usuario actual desde 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'
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
// Consultar la API de Authentik para obtener información detallada del usuario
|
||||
const response = await $fetch(`${authentikUrl}/api/v3/core/users/?username=${username}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${authentikToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
// La API devuelve un array de resultados
|
||||
const users = response as any
|
||||
|
||||
if (!users.results || users.results.length === 0) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
message: 'Usuario no encontrado'
|
||||
})
|
||||
}
|
||||
|
||||
// Devolver el primer resultado (debería ser único por username)
|
||||
return users.results[0]
|
||||
} catch (error: any) {
|
||||
console.error('Error al obtener usuario de Authentik:', error)
|
||||
throw createError({
|
||||
statusCode: error.statusCode || 500,
|
||||
message: error.message || 'Error al obtener información del usuario'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -1,74 +0,0 @@
|
||||
/**
|
||||
* 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'
|
||||
})
|
||||
}
|
||||
})
|
||||
103
nuxt4/app/server/api/authentik/user.ts
Normal file
103
nuxt4/app/server/api/authentik/user.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* API endpoint para gestionar información del usuario en Authentik
|
||||
* Soporta GET (obtener) y PATCH (actualizar)
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const headers = getRequestHeaders(event)
|
||||
const method = event.method
|
||||
|
||||
// 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'
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
if (method === 'GET') {
|
||||
// GET: Obtener información del usuario
|
||||
const response = await $fetch(`${authentikUrl}/api/v3/core/users/?username=${username}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${authentikToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
const users = response as any
|
||||
|
||||
if (!users.results || users.results.length === 0) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
message: 'Usuario no encontrado'
|
||||
})
|
||||
}
|
||||
|
||||
return users.results[0]
|
||||
}
|
||||
else if (method === 'PATCH') {
|
||||
// PATCH: Actualizar información del usuario
|
||||
const body = await readBody(event)
|
||||
|
||||
// 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,
|
||||
...(body.email && { email: body.email })
|
||||
}
|
||||
})
|
||||
|
||||
return updateResponse
|
||||
}
|
||||
else {
|
||||
throw createError({
|
||||
statusCode: 405,
|
||||
message: 'Método no permitido'
|
||||
})
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error en API de Authentik:', error)
|
||||
throw createError({
|
||||
statusCode: error.statusCode || 500,
|
||||
message: error.message || 'Error al procesar la petición'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user