question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Cors to callable function

See original GitHub issue

I wrote a callable function to invite new users for my app:

exports.UserInvitation = functions.https.onCall((data, context) => {
  const email = data.email
  const password = passwordGenerator.generate({
    length: 12,
    numbers: true
  })
  console.log('Inviting user:', email)
  const mailOptions = {
    from: '',
    to: email,
    subject: ``,
    html: utils.formatEmail(`
     Hello, you've been invited to use my app.
    `)
  }

  return new Promise((resolve, reject) => {
    admin.auth().createUser({
      email: email,
      emailVerified: false,
      password: password
    }).then((user) => {
      console.log(`Created user with uid ${user.uid}`)
      transporter.sendMail(mailOptions).then(() => {
        console.log(`Email sent to ${email}`)
        resolve()
      })
    }).catch((err) => {
      console.error(err.code)
      reject(new functions.https.HttpsError(err.code, err.message))
    })
  })
})

I call it like this in my app:

functions.httpsCallable('UserInvitation')({ email: this.input.value })

But I’m getting “No Access Control Allow Origin” error when trying to call it from client. How can I fix this if I don’t have access to req/res objects inside onCall, to enable cors?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:13
  • Comments:44 (2 by maintainers)

github_iconTop GitHub Comments

35reactions
marimanocommented, Mar 27, 2020

I recently faced the CORS error only for newly created functions. After hours of desperate search I found that newly created functions don’t have Cloud Functions Invoker at https://console.cloud.google.com/functions while the old ones have allUsers: image

Here firebase mentions that starting from Jan 15, 2020 no allUsers is set anymore https://cloud.google.com/functions/docs/securing/managing-access-iam

11reactions
8tomo8commented, May 29, 2019

@tricaricom @nicholmikey The CORS error in console with callable functions only indicates there is an error in the cloud function. Check the logs inside the Firebase console and you’ll see the actual error. CORS is not applicable for callable functions even when running on localhost

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase Callable Function + CORS - Stack Overflow
Go to the cloud function tab · Select your cloud function (check box) · Click "Add members" under Permissions tab in the right...
Read more >
CORS issue with https callable function : r/Firebase - Reddit
Hi everyone. Been stuck on this issue for a couple days, searching the web and can't seem to find a solution.
Read more >
Call functions via HTTP requests | Cloud Functions for Firebase
Configuring CORS (Cross-Origin Resource Sharing) ... Use the cors option to control which origins can access your function. By default, HTTP functions don't...
Read more >
You can give this one a read. It's unlikely to be a CORS issue ...
It's unlikely to be a CORS issue for onCall function.. https://stackoverflow.com/questions/50278537/firebase-callable-function-cors.
Read more >
google cloud function has been blocked by cors policy
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. Open side panel....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found