Feat: Middleware CORS para manejar preflight requests
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m8s
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m8s
El problema era que Authentik interceptaba las solicitudes OPTIONS antes de llegar a la app. Ahora el middleware 00.cors.ts: - Se ejecuta antes de 01.api-auth.ts - Responde OPTIONS con 204 y headers CORS - Permite solicitudes cross-origin desde cualquier dominio
This commit is contained in:
38
server/middleware/00.cors.ts
Normal file
38
server/middleware/00.cors.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* Middleware CORS - Se ejecuta ANTES de la autenticación
|
||||||
|
*
|
||||||
|
* Maneja las solicitudes OPTIONS (preflight) para rutas de API externa
|
||||||
|
* y agrega headers CORS a las respuestas.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Rutas que necesitan CORS (API externa)
|
||||||
|
const CORS_ROUTES = [
|
||||||
|
'/api/messages/send',
|
||||||
|
'/api/mcp'
|
||||||
|
]
|
||||||
|
|
||||||
|
export default defineEventHandler((event) => {
|
||||||
|
const path = getRequestURL(event).pathname
|
||||||
|
|
||||||
|
// Solo aplicar a rutas que necesitan CORS
|
||||||
|
const needsCors = CORS_ROUTES.some(route => path.startsWith(route))
|
||||||
|
|
||||||
|
if (!needsCors) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Agregar headers CORS a todas las respuestas de estas rutas
|
||||||
|
setResponseHeaders(event, {
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
||||||
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||||
|
'Access-Control-Max-Age': '86400' // Cache preflight por 24 horas
|
||||||
|
})
|
||||||
|
|
||||||
|
// Si es una solicitud OPTIONS (preflight), responder inmediatamente
|
||||||
|
if (getMethod(event) === 'OPTIONS') {
|
||||||
|
// Responder con 204 No Content
|
||||||
|
setResponseStatus(event, 204)
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user