"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const inventory_functions = require("firebase-functions");
const inventory_admin = require("firebase-admin");
import * as cors from "cors";
const corsHandler = cors({ origin: true });
exports.getByLocation = inventory_functions.https.onRequest(
(request, response) => {
corsHandler(request, response, async () => {
const inventory = await inventory_admin
.firestore()
.collection("inventory")
.get();
response.status(200).send(JSON.stringify(inventory));
});
}
);
Here is how i am trying to access from client:
const res = await fetch(
"https://us-central1-surplus-functions.cloudfunctions.net/inventory-getByLocation"
);
if (!res.ok) throw new Error(res.statusText);
return res.json();
Here is error:
Access to fetch at 'https://[project id].cloudfunctions.net/inventory-getByLocation' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I verified i am not seeing response headers in postman that i expect even though i am setting them in the above code so confused on why that is.
I now have a working solution:
const authentication_cors = require("cors")({
origin: true,
});
exports.register = authentication_functions.https.onRequest(
async (request: any, response: any) => {
return authentication_cors(request, response, async () => {
const data = JSON.parse(request.body);
const resp = await AuthenticationBO.Register(
authentication_admin,
data.Profile.Email,
data.Vendor,
data.Profile,
data.Vendor?.Name
);
response.send(resp);
});
});