@@ -40,6 +40,7 @@ import {bindNotifications} from "../system/bindNotifications";
4040import { INotification } from "../interfaces/types" ;
4141import { MediaManagerHandler } from "./media-manager/MediaManagerHandler" ;
4242import { StorageServices } from "./catalog/Navigation" ;
43+ import { bindCors } from "../system/bindCors" ;
4344
4445export class Adminizer {
4546 // Preconfigures
@@ -212,60 +213,9 @@ export class Adminizer {
212213 }
213214 } ;
214215
215- // Middleware для всех API маршрутов
216- const defaultOrigin = process . env . FRONTEND_URL || 'http://localhost:8080' ;
216+ // Bind cors
217+ bindCors ( this )
217218
218- if ( config ?. cors ?. enabled ) {
219- const corsConfig = config . cors ;
220-
221- // Поддерживаем массив разрешенных origin
222- const allowedOrigins = Array . isArray ( corsConfig . origin )
223- ? corsConfig . origin
224- : [ corsConfig . origin || defaultOrigin ] ;
225-
226- this . app . all ( `${ this . config . routePrefix } /api/*` , ( req : any , res : any , next : any ) => {
227- const requestOrigin = req . headers . origin ;
228-
229- // Проверяем разрешен ли origin
230- const isOriginAllowed = ! requestOrigin || allowedOrigins . includes ( requestOrigin ) ;
231-
232-
233- if ( requestOrigin && ! isOriginAllowed ) {
234- console . log ( `❌ CORS: Blocked request from ${ requestOrigin } ` ) ;
235-
236- if ( req . method === 'OPTIONS' ) {
237- // Для preflight - 200 без CORS headers
238- return res . status ( 200 ) . end ( ) ;
239- } else {
240- // Для основных запросов - ошибка
241- return res . status ( 403 ) . json ( {
242- error : 'CORS policy: Origin not allowed'
243- } ) ;
244- }
245- }
246-
247- // Запрос с разрешенного origin или без Origin
248- if ( isOriginAllowed ) {
249- // Для CORS запросов возвращаем тот же origin (или первый из списка)
250- const allowOrigin = requestOrigin || allowedOrigins [ 0 ] ;
251- res . header ( 'Access-Control-Allow-Origin' , allowOrigin ) ;
252- res . header ( 'Access-Control-Allow-Credentials' ,
253- corsConfig . credentials !== false ? 'true' : 'false' ) ;
254- res . header ( 'Access-Control-Allow-Methods' ,
255- corsConfig . methods ?. join ( ',' ) || 'GET,POST,PUT,DELETE,OPTIONS' ) ;
256- res . header ( 'Access-Control-Allow-Headers' ,
257- corsConfig . allowedHeaders ?. join ( ',' ) || 'Content-Type,Authorization,X-Requested-With,X-CSRF-Token,x-xsrf-token' ) ;
258- }
259-
260- if ( req . method === 'OPTIONS' ) {
261- return res . status ( 200 ) . end ( ) ;
262- }
263-
264- next ( ) ;
265- } ) ;
266-
267- console . log ( '✅ API CORS middleware enabled. Allowed origins:' , allowedOrigins ) ;
268- }
269219 // set cookie parser
270220 this . app . use ( cookieParser ( ) ) ;
271221
0 commit comments