Closed Assowavesss closed 1 year ago
If the filter rejects your request, the post-processing advice will not be invoked, as shown in the image below:
You could try to add the header to responses in the filter callback, or use the PreSendingAdvice.
Yes I saw this schema earlier, I have just find a way.
Here is my original filter
void SignInFilter::doFilter(const HttpRequestPtr& req,
FilterCallback&& fcb,
FilterChainCallback&& fccb)
{
if (const auto& jsonRequest = req->getJsonObject();
jsonRequest &&
jsonRequest->size() == mandatoryLineNumber &&
jsonRequest->isMember(Account::Cols::_email)
)
{
fccb();
}
else
{
fcb(createHttpResponse(std::move(createJsonResponse(
std::make_pair("code", "UNAUTHORIZED"),
std::make_pair("message", "Unauthorized access")
)),
k401Unauthorized
));
}
}
I just add a verification for option method
void SignInFilter::doFilter(const HttpRequestPtr& req,
FilterCallback&& fcb,
FilterChainCallback&& fccb)
{
if (const auto& jsonRequest = req->getJsonObject();
req->method() == Options || /* Just here */
jsonRequest &&
jsonRequest->size() == mandatoryLineNumber &&
jsonRequest->isMember(Account::Cols::_email)
)
{
fccb();
}
else
{
fcb(createHttpResponse(std::move(createJsonResponse(
std::make_pair("code", "UNAUTHORIZED"),
std::make_pair("message", "Unauthorized access")
)),
k401Unauthorized
));
}
}
And fixed my issue but I have to keep this code in the main
drogon::app()
.registerPostHandlingAdvice(
[](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp) {
//LOG_DEBUG << "postHandling1";
resp->addHeader("Access-Control-Allow-Origin", "*");
})
I got this error : With the following code The url of my front end is http://localhost:3000
And I added this function in the main to handle the cors policy
Here is my axios post method
PS: I don't know why but, I have no cors error with this handler
When I delete the filter, it works. Should handle the cors inside filter for option method ? or the api should make this for me ?
Please help me !