1+ // CorsFilter.cpp
12#include " filters/CorsFilter.h"
23#include " utils/Logger.h"
34
@@ -7,15 +8,15 @@ namespace api {
78void CorsFilter::doFilter (const drogon::HttpRequestPtr& req,
89 drogon::FilterCallback&& fcb,
910 drogon::FilterChainCallback&& fccb) {
10-
1111 API_LOG_DEBUG (" Processing CORS request for path: {}" , req->getPath ());
1212
13+ // Create response for handling CORS
1314 auto resp = drogon::HttpResponse::newHttpResponse ();
1415
15- // Add CORS headers
16- resp->addHeader (" Access-Control-Allow-Origin" , " * " );
17- resp->addHeader (" Access-Control-Allow-Methods" , " OPTIONS, GET,POST,HEAD " );
18- resp->addHeader (" Access-Control-Allow-Headers" , " x-requested-with,content-type " );
16+ // Add CORS headers matching your config
17+ resp->addHeader (" Access-Control-Allow-Origin" , " http://localhost:3000 " );
18+ resp->addHeader (" Access-Control-Allow-Methods" , " GET,POST,OPTIONS " );
19+ resp->addHeader (" Access-Control-Allow-Headers" , " Content-Type,Authorization " );
1920 resp->addHeader (" Access-Control-Max-Age" , " 86400" );
2021
2122 // Handle preflight request
@@ -26,7 +27,13 @@ void CorsFilter::doFilter(const drogon::HttpRequestPtr& req,
2627 return ;
2728 }
2829
29- // For non-OPTIONS requests, continue with the chain but with CORS headers
30+ // For non-OPTIONS requests, add CORS headers to the response
31+ req->addHeader (" Access-Control-Allow-Origin" , " http://localhost:3000" );
32+ req->addHeader (" Access-Control-Allow-Methods" , " GET,POST,OPTIONS" );
33+ req->addHeader (" Access-Control-Allow-Headers" , " Content-Type,Authorization" );
34+ req->addHeader (" Access-Control-Max-Age" , " 86400" );
35+
36+ // Continue with the request chain
3037 fccb ();
3138}
3239
0 commit comments