I'm not able to correctly handle CORS issues when doing either PATCH
/POST
/PUT
requests from the browser sending an Authorization
header with a Bearer token
(this works correctly outside of the browser and for GET
requests) in Zeit Now serverless.
I'm using Auth0 for the authorization side if that helps.
This is my now.json
headers section, I've tried a lot of combinations for these, but neither succeeded from the browser.
cors
package without successroutes
in now.json
res.addHeader()
OPTIONS
request manually doing variations of this:Finally, this is the error that I get
Access to XMLHttpRequest at 'https://api.example.org/api/users' from origin 'https://example.org' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Not sure what I'm wrong or how to handle this properly.
Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.
Cross-Origin Resource Sharing (CORS) is a standard that allows a server to relax the same-origin policy. This is used to explicitly allow some cross-origin requests while rejecting others. For example, if a site offers an embeddable service, it may be necessary to relax certain restrictions.
I have pretty much similar issues with CORS and Vercel serverless function.
After lots of try → failed process I just found solutions for this.
The simplest solution, just using micro-cors.
And having an implementation something like;
import { NowRequest, NowResponse } from '@now/node';
import microCors from 'micro-cors';
const cors = microCors();
const handler = (request: NowRequest, response: NowResponse): NowResponse => {
if (request.method === 'OPTIONS') {
return response.status(200).send('ok');
}
// handle incoming request as usual
};
export default cors(handler);
using vercel.json
to handle request headers
vercel.json
{
"headers": [
{
"source": "/.*",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Headers",
"value": "X-Requested-With, Access-Control-Allow-Origin, X-HTTP-Method-Override, Content-Type, Authorization, Accept"
},
{
"key": "Access-Control-Allow-Credentials",
"value": "true"
}
]
}
]
}
After self tried, there are 2 keys important in an above setting,
Access-Control-Allow-Origin
as what you wantAccess-Control-Allow-Headers
you must include Access-Control-Allow-Origin
into its value.then in serverless function, you still need to handle pre-flight request as well
/api/index.ts
const handler = (request: NowRequest, response: NowResponse): NowResponse => {
if (request.method === 'OPTIONS') {
return response.status(200).send('ok');
}
// handle incoming request as usual
};
I would suggest to read through the code in micro-cors, it's very simple code, you can understand what it'll do in under few minutes, which makes me didn't concern about adding this into my dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With