firebase / firebase/functions-samples
authorized-https-endpoint doesn't work for CORS using cookies (with solution)
- Dominant language
- JavaScript
- Stars
- 12.2k
- Forks
- 3.8k
- Avg merge
- 3d 23h
- Merged PRs (30d)
- 1
Description
CORS doesn't work if we are running the client from a different host unless we configure it properly. So while passing the auth token via the "headers" work, passing the auth token via the "cookies" will not.
### How to reproduce these conditions
1. Run `firebase serve --only functions` (Don't run the hosting part, just the functions)
2. **Run the front-end separately (not through firebase serve)**. So copy everything in the `/public` folder to somewhere else and then, may run it as a static file like so: `python -m SimpleHTTPServer`. This will run the code the front end on `localhost:8000`
3. Sign in using Google, you'll see that the "headers" test works, but "cookies" test breaks.
### Solution
1. In the front end, add `req.withCredentials = true`, this will allow browsers to send cookies cross-domain.
```
Demo.prototype.startFunctionsCookieRequest = function() {
var req = new XMLHttpRequest();
req.withCredentials = true; //← Add this line 👈🏼
...
..
}
```
2. In the backend, add `cretentials: true` to the cors object like below. This will allow the server to accept cookies coming from cross-domain.
```
const cors = require("cors")({
origin: true,
credentials: true
});
```
Contributor guide
Assessment
This issue has not been assessed yet.