diff --git a/nuxt4/app/server/api/authentik/user.get.ts b/nuxt4/app/server/api/authentik/user.get.ts deleted file mode 100644 index 0e5ec69..0000000 --- a/nuxt4/app/server/api/authentik/user.get.ts +++ /dev/null @@ -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' - }) - } -}) diff --git a/nuxt4/app/server/api/authentik/user.patch.ts b/nuxt4/app/server/api/authentik/user.patch.ts deleted file mode 100644 index dd4b627..0000000 --- a/nuxt4/app/server/api/authentik/user.patch.ts +++ /dev/null @@ -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' - }) - } -}) diff --git a/nuxt4/app/server/api/authentik/user.ts b/nuxt4/app/server/api/authentik/user.ts new file mode 100644 index 0000000..a46158e --- /dev/null +++ b/nuxt4/app/server/api/authentik/user.ts @@ -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' + }) + } +})