diff --git a/nuxt4-app/app/pages/login.vue b/nuxt4-app/app/pages/login.vue index f78c062..31ee0d4 100644 --- a/nuxt4-app/app/pages/login.vue +++ b/nuxt4-app/app/pages/login.vue @@ -8,8 +8,8 @@ const route = useRoute() const redirectPath = route.query.redirect as string || '/' const login = () => { - // Navegar a la ruta de OAuth - navigateTo('/auth/authentik') + // Navegar a la ruta de OAuth (ahora en /api/auth/authentik) + navigateTo('/api/auth/authentik') } diff --git a/nuxt4-app/server/routes/auth/authentik.get.ts b/nuxt4-app/server/api/auth/authentik.get.ts similarity index 95% rename from nuxt4-app/server/routes/auth/authentik.get.ts rename to nuxt4-app/server/api/auth/authentik.get.ts index 022c08c..646ac64 100644 --- a/nuxt4-app/server/routes/auth/authentik.get.ts +++ b/nuxt4-app/server/api/auth/authentik.get.ts @@ -1,10 +1,9 @@ import { getQuery } from 'h3' import { withQuery } from 'ufo' -import { defu } from 'defu' /** * OAuth Authentik Login Handler - * Ruta: /auth/authentik + * Ruta: /api/auth/authentik * * Este endpoint inicia el flujo OAuth con Authentik */ @@ -73,7 +72,7 @@ export default defineEventHandler(async (event) => { return sendRedirect(event, '/') } catch (error: any) { console.error('Authentik OAuth error:', error) - return sendRedirect(event, '/error?message=auth_failed') + return sendRedirect(event, '/?error=auth_failed') } } diff --git a/nuxt4-app/server/routes/auth/logout.get.ts b/nuxt4-app/server/api/auth/logout.get.ts similarity index 89% rename from nuxt4-app/server/routes/auth/logout.get.ts rename to nuxt4-app/server/api/auth/logout.get.ts index f17fc4a..8cd74a4 100644 --- a/nuxt4-app/server/routes/auth/logout.get.ts +++ b/nuxt4-app/server/api/auth/logout.get.ts @@ -1,6 +1,6 @@ /** * Logout Handler - * Ruta: /auth/logout + * Ruta: /api/auth/logout * * Limpia la sesión del usuario y redirige a la página de inicio */ diff --git a/nuxt4-app/server/api/debug-config.get.ts b/nuxt4-app/server/api/debug-config.get.ts.bak similarity index 100% rename from nuxt4-app/server/api/debug-config.get.ts rename to nuxt4-app/server/api/debug-config.get.ts.bak diff --git a/nuxt4-app/server/routes/auth.authentik.get.ts b/nuxt4-app/server/routes/auth.authentik.get.ts deleted file mode 100644 index 022c08c..0000000 --- a/nuxt4-app/server/routes/auth.authentik.get.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { getQuery } from 'h3' -import { withQuery } from 'ufo' -import { defu } from 'defu' - -/** - * OAuth Authentik Login Handler - * Ruta: /auth/authentik - * - * Este endpoint inicia el flujo OAuth con Authentik - */ -export default defineEventHandler(async (event) => { - const runtimeConfig = useRuntimeConfig(event) - const query = getQuery(event) - - // Configuración de Authentik - const config = { - clientId: runtimeConfig.oauth.authentik.clientId, - clientSecret: runtimeConfig.oauth.authentik.clientSecret, - serverUrl: runtimeConfig.oauth.authentik.serverUrl, - redirectURL: runtimeConfig.oauth.authentik.redirectURL, - scope: ['openid', 'profile', 'email'], - } - - console.log('OAuth Authentik - Iniciando flujo:', { - serverUrl: config.serverUrl, - redirectURL: config.redirectURL, - hasCode: !!query.code - }) - - // Handle OAuth callback - if (query.code) { - try { - // Exchange code for tokens - const tokenUrl = `${config.serverUrl}/application/o/token/` - const tokenResponse = await $fetch(tokenUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - body: new URLSearchParams({ - grant_type: 'authorization_code', - client_id: config.clientId, - client_secret: config.clientSecret, - code: query.code as string, - redirect_uri: config.redirectURL, - }), - }) - - const tokens = tokenResponse as any - - // Get user info - const userInfoUrl = `${config.serverUrl}/application/o/userinfo/` - const user = await $fetch(userInfoUrl, { - headers: { - Authorization: `Bearer ${tokens.access_token}`, - }, - }) - - // Guardar información del usuario en la sesión - await setUserSession(event, { - user: { - id: (user as any).sub, - email: (user as any).email, - name: (user as any).name || (user as any).preferred_username, - username: (user as any).preferred_username, - picture: (user as any).picture, - groups: (user as any).groups || [] - }, - loggedInAt: Date.now() - }) - - // Redirigir al dashboard después del login - return sendRedirect(event, '/') - } catch (error: any) { - console.error('Authentik OAuth error:', error) - return sendRedirect(event, '/error?message=auth_failed') - } - } - - // Initial redirect to Authentik - const authorizationUrl = withQuery( - `${config.serverUrl}/application/o/authorize/`, - { - client_id: config.clientId, - redirect_uri: config.redirectURL, - response_type: 'code', - scope: config.scope.join(' '), - } - ) - - console.log('Redirecting to:', authorizationUrl) - return sendRedirect(event, authorizationUrl) -}) diff --git a/nuxt4-app/server/routes/auth.logout.get.ts b/nuxt4-app/server/routes/auth.logout.get.ts deleted file mode 100644 index f17fc4a..0000000 --- a/nuxt4-app/server/routes/auth.logout.get.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Logout Handler - * Ruta: /auth/logout - * - * Limpia la sesión del usuario y redirige a la página de inicio - */ -export default defineEventHandler(async (event) => { - await clearUserSession(event) - return sendRedirect(event, '/') -})