Firebase hosting connect to cloud function in *europe-west1* - firebase

I have an api set up on Google Cloud Functions (https://europe-west1-myproject-name.cloudfunctions.net/api/v1/ical.ics).
This works well, but I wish to set up a "friendly" domain name for the api. :)
According to Googles documentation this is seems easy, but it does not seem to work for cloud functions outside the USA, eg. europe-west1.
I have updated the firebase.json file with the below code according to documentation.
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/**",
"function": "api"
},
{
"source": "**",
"destination": "/index.html"
}
]
}
When accessing https://myproject-name.web.app/api/v1/ical.ics
I get redirected to https://us-central1-myproject-name.cloudfunctions.net/api/api/v1/cal.ics with error 403 and the below error message.
Error: Forbidden
Your client does not have permission to get URL /api/api/ical.ics from this server.
I must be overlooking something really basic here, since this seems like a really easy operation? :)
Kind regards
/K

As stated in the documentation (see blue text block):
If you are using HTTP functions to serve dynamic content for Firebase
Hosting, you must use us-central1.
You will also find a similar warning in the doc you refer to in your question about "Serve dynamic content and host microservices with Cloud Functions" (See blue text block as well):
Firebase Hosting supports Cloud Functions in us-central1 only.

It is not completely the answer you are looking for since it is not possible with Firebase Hosting.
But it is possible to get a custom domain in front of your cloud functions hosted in EU by using Cloud Run.
I followed this guide:
https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-functions
And after that i added the custom domain under the Manage custom domains on Cloud Run.

Related

POST to a firebase function behind custom domain

We are trying to build a REST api using firebase functions. We want that format because we are a web services company who need to expose an api to our clients. People are mostly familiar with REST, therefore, REST.
Following the advice given here I have layered a custom domain in front of a firebase function. I did this by first forwarding my custom domain to the firebase domain via an A record,then setting up a rewrite for the hosting:
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/**",
"function": "api"
},
{
"source": "!/#(api)/**",
"destination": "/index.html"
}
]
},
Note that the api function is mapped to anything going to /api/. So far so good. Now I want to make a POST request with a body to mydomain.com/api/user. Unfortunately, when doing so, I get a response Cannot GET /api/user. It seems like firebase is converting the request into a GET before it reaches the function at api. Does anyone know how to make POST requests to functions behind custom domains?
In case someone stumbles over this it's because our route was not defined as containing api. We had something like /user but this configuration obviously requires /api/user.

Can I view the configuration for the Firebase functions used by my website using Firebase/Google Cloud's UI?

I deployed a new version of a web app that included some new Firebase functions. The new Firebase functions were inaccessible upon deployment.
I was able to find the root cause by familiarity with Firebase's configuration: the function requests were intended to route through the main app domain, and be redirected on the Firebase server to their final destination. (This should have been set up with Firebase's 'rewrites' section of firebase.json, but wasn't.)
A partial view of my rewrites section of firebase.json:
"rewrites": [
{
"source": "/getPlaces",
"function": "getPlaces"
},
...
{
"source": "**",
"destination": "/index.html"
}
]
In plain English, this says, "Firebase server: every time you get a request routing to https://my.app/getPlaces, I want you to not route to that address within my app; I want to invoke my Cloud function instead. Otherwise, route normally."
Yet when I look at the Functions tab of my Firebase console, all I see is this:
Under 'Request', it says https://us-central1-my-app.cloudfunctions.net/getPlaces. That gives me just one of the ways to access my function; the other is https://my.app/getPlaces, as defined in rewrites. I need to know all of the addresses that Firebase will respond to, not just the default one using cloudfunctions.net.
Is it possible to see the entire configuration for the deployed Firebase functions anywhere in the web UI, ie console.firebase.com or console.cloud.google.com, where redirects from rewrites like this can be seen?

Send parameters to Cloud Functions with Firebase Hosting

I'm using two firebase services : Cloud Functions & Firebase hosting.
According to the documentation I'm able to configure urls on Firebase hosting by using firebase.json :
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "showWebProduct"
}
]
}
}
So, in my case when anything is used on path URL it executes showWebProduct, but is it possible to send parameters to this Cloud function ? I don't fond any ressource which show an example with parameter in query.
Thanks
It's not possible to specify parameters in the Hosting configuration to be included in the call to your function. Instead, you should direct Hosting to a function that already has everything it needs to operate.
If you have a specific use case that demands it, you should file a feature request with Firebase support.

Use custom domain for firebase function http calls

Is there a way to use a custom domain for firebase cloud functions http hooks.
The default url for cloud functions looks something like this:
https://us-central1-my-awesome-app.cloudfunctions.net/ios-oauth/
And
I would like to make it look like this:
https://myawesomeapp.com/ios-oauth/
I looked around if there was some other people looking for the same solution and sure enough I found this:
https://stackoverflow.com/questions/43482224/firebase-cloud-functions-custom-domain
I have contacted firebase support to get some answers about this.
And I was forwarded to this part in the documentation.
https://firebase.google.com/docs/hosting/functions#create_an_http_function_to_your_hosting_site
You can use your own domain with the firebase-cloud-functions. The way to do is is using the firebase-hosting.
Connect custom domain to firebase hosting
Add custom function routing to firebase.json
{
"hosting": {
"public": "public",
// Add the following rewrites section *within* "hosting"
"rewrites": [{
"source": "/bigben", "function": "bigben"
}]
}
}
Deploy to firebase
The accepted answer is correct, and I created this repository last year to demonstrate the functionality: https://github.com/cjmyles/firebase-react-express
In order to retain the HTML pushState functionality as per https://firebase.google.com/docs/hosting/full-config#rewrites you may want to extend the rules to allow all other requests through to your index.html page, which solves the issue #Boris was facing in his answer.
"rewrites": [
{
"source": "/api/**",
"function": "app"
},
{
"source": "!/#(api)/**",
"destination": "/index.html"
}
]
This shouldn't really be needed as the rewrite rules are meant to match the first occurence of a request (so the order matters), but this worked for me by allowing all non-api related requests through.
Please note: At the time of writing this the Firebase documentation states: Firebase Hosting supports Cloud Functions in us-central1 only.
If anyone else runs into this, Thomas Bulva's answer is correct but for me, I also had to remove the below snippet from the firebase.json file. .
It was redirecting any request to the index.html page. My https://us---<>.cloudfunctions.net URL was working fine; when I did /helloWorld it would take me to "Hello from Firebase!" But if I tried the same from my custom domain, it would fail. Removing this fixed it.
{
"source": "**",
"destination": "/index.html"
},

Cloud Functions for Firebase not setting Access-Control-Allow-Origin

While attempting to get CORS working with Cloud Functions for Firebase, I tried to follow the official example here: https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint
However, even following the tutorial instructions as exactly as possible, I still get this error in the console:
I never got CORS working, but the workaround I found was to use hosting redirects for the functions. That way, HTTP function calls are not cross site at all.
https://firebase.google.com/docs/hosting/functions (See "Direct Hosting requests to your function")
{
"hosting": {
"public": "public",
// Add the following rewrites section *within* "hosting"
"rewrites": [ {
"source": "/api/bigben", "function": "bigben"
} ]
}
}
Note: I have to define a redirect for every endpoint. Is there a way to have general rewrites? e.g.
"source": "/api/*", "function": "*"
Try following this ?
https://stackoverflow.com/a/42756623/6650162
Handling CORS in Google Cloud Functions
Hope that helps!

Resources