I have a problem with Firebase and CORs, apparently it cannot reach the endpoint with errors like:
Access to fetch at 'https://europe-west2-XXX.cloudfunctions.net/fetchChatToken' from origin 'https://trato.app' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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. service.ts:203
POST https://europe-west2-XXX.cloudfunctions.net/fetchChatToken net::ERR_FAILED (anonymous) @ service.ts:203 ... ...
error.ts:66 Uncaught (in promise) Error: internal at new t (error.ts:66) at error.ts:175 at e. (service.ts:276) at tslib.es6.js:100 at Object.next (tslib.es6.js:81) at a (tslib.es6.js:71)
I also checked the network tab on dev inspector (chrome) to check if the CORS header is there, i dont see it.
Also, I have been checking firebase functions logs and apparently is not being even invoked, the last line showing is the deployment.
the way that Im using it is this:
Front End side:
const functions = firebaseApp.functions('europe-west2');
export const fetchChatToken = async () => (await functions.httpsCallable('fetchChatToken')()).data;
Functions (Backend) side:
const ensureAuthentication = auth => { if (!auth) throw new HttpsError("unauthenticated", "authentication required"); };
exports.fetchChatToken = functions.region("europe-west2").https.onCall((data, context) => {
ensureAuthentication(context.auth);
try {
const { AccessToken } = twilio.jwt;
const { ChatGrant } = AccessToken;
const grant = new ChatGrant({
serviceSid: conversationsid
});
const token = new AccessToken(accountsid, apikey, apisecret);
token.addGrant(grant);
token.identity = context.auth.uid;
return token.toJwt();
} catch (error) {
console.error(error);
throw new HttpsError("internal", "internal error");
} });