Firebase cloud functions issue with cross origin in preflight request

958 views
Skip to first unread message

György Kiss

unread,
Apr 22, 2020, 10:42:12 AM4/22/20
to Firebase Google Group
One of our cloud function get's back with the following error:

"Access to fetch at 'https://us-central1-tribe-staging.cloudfunctions.net/getProfile' from origin (either localhost, or firebase hosting does the same too) 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"

the cloud function itself does work correctly if I test it from the google cloud console but I can't reach it from our react app.
In the cloud functions cross origin is explicitly allowed with the following:
const cors = require('cors')({ origin: true });

the cloud function:



exports.getProfile = functions.https.onCall((data, context) => {
console.log(data)
// required
var currentUser = context.auth.uid;
if (!currentUser) throw new Error('User not logged in');


if (!data.userId) throw new Error('no uid sent');

// create DB object and sync transaction
return admin.auth().getUser(data.userId)
.then(function (userRecord) {
let userData
let user
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully fetched user data:', userRecord);
user = userRecord

console.log(user)
return (user)
})
.catch(function (error) {
console.log('Error fetching user data:', error);
});



});

in react the firebase app is inited in react context in the following way:

class Firebase {
constructor() {
app.initializeApp(firebaseConfig);
this.auth = app.auth()
this.db = app.firestore()
this.storage = app.storage()
this.functions = app.functions()
}

}

export default Firebase;

so that the functions can be reach through porps


the call from react app:

const profileRef = t.firebase.functions.httpsCallable('getProfile')
console.log(profileRef)

profileRef({userId: id})


If I call a different cloud function from the same place it does work correctly.

renaming the function, redeploying, deploying from different compuetr or vm did not help


Reply all
Reply to author
Forward
0 new messages