firebase-backend icon indicating copy to clipboard operation
firebase-backend copied to clipboard

Add Cors options for Restful Endpoints

Open FilledStacks opened this issue 3 years ago • 6 comments

The package uses the express router. We should probably be able to enable CORS like in Google's example: https://firebase.google.com/docs/functions/http-events#using_existing_express_apps

Or somehow be able to define CORS configuration for each function / route: https://github.com/expressjs/cors#enable-cors-for-a-single-route

This can be implemented either or in both ways but it is something that will be required. I don't know the security concerns with setting cors enabled to true for all functions so:

  • We should default to cors enabled false
  • Add an option to the function parser to set the default true for all Restful functions.

This will make it so that the devs that want it on for everything can do that, the ones that don't won't have to change anything.

FilledStacks avatar Mar 28 '21 14:03 FilledStacks

PR has been made. It's been stuck on review for a while. Have requested a readme update as well and it'll be merged in.

FilledStacks avatar Apr 14 '21 03:04 FilledStacks

Looks great!

smakman avatar Apr 14 '21 09:04 smakman

Any update on this?

smakman avatar Jun 04 '21 11:06 smakman

I'm having some problems with CORS, im using Firebase Hosting and the functions show this error when I try to call them:

image

my cors config is this: image

any idea?

ejuvaleru avatar Jun 17 '21 04:06 ejuvaleru

I am still getting cors errors (using v0.2.4). Can someone verify that the new enableCors setting actually works?

mg98 avatar Oct 22 '21 21:10 mg98

Ah sorry my fault, I typed the wrong URL in my request. I can say it works :)

@ejuvaleru Since you "liked" my post, make sure you are running version 0.2.4 and put {enableCors: true} either on your FunctionParser (index.ts) or on your endpoint.

Examples:

exports = new FunctionParser(__dirname, exports, { enableCors: true }).exports;
export default new Post(async (request: Request, response: Response) => { ... }, { enableCors: true })

mg98 avatar Oct 23 '21 08:10 mg98