Authentication problem with google cloud function gen2 in golang on firebase routing - firebase

Im testing go gen2 google cloud functions, the code of the function was deployed ok, it works ok, everything is fine. The function is named readrequest, and it is open to everybody. You can reach it https://readrequest-v7disnxdea-uc.a.run.app
I also add mapping to a dedicated subdomain without problem.
Now I'm trying to route in one web firebase app. For that I've added a route to firebase.json
the rules in firebase.json looks like:
{
"hosting": {
"public": "public",
"rewrites": [
{
"source": "/l",
"function": "l3"
},
{
"source": "/gotest",
"function": "readrequest"
},
{
"source": "/gotest/**",
"function": "readrequest"
},
{
"source": "/rc1",
"function": "rc1"
},
{
"source": "/rc2",
"function": "rc2"
},
{
"source": "/rc2/**",
"function": "rc2"
},
{
"source": "/rc1/**",
"function": "rc1"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
I can reach the funcion, always ask for authentication, but it doesn't work even if I accept to authenticate. I've created the function in us-central1 to avoid problems.
The error is:
Error: Forbidden
Your client does not have permission to get URL /readrequest/gotest/asdf from this server.
If I misspell the function name I get the same error, so it is like the router isn't locating the function, but don't why not.

Because Cloud Functions gen2 runs on top of Cloud Run (url, permission, logs, and many other things are the same!), the way to reach Cloud Functions gen2 is not the usual Cloud Functions gen1 manner, it's the Cloud Run one!
Buy the way, replace the function (gen1) reference "function": "readrequest", the functions (gen2, i.e. Cloud Run) reference "run": { "serviceId": "readrequest", "region": "us-central1" }

Related

How to upload app to subdomain in Firebase

I have 2 subdomains configurated on firebase, myapp1.mydomain.cl and myapp2.mydomain.cl.
My default site url provided by Firebase are mydomain-1100.web.app
Im trying to deploy myapp1 into a subdomain with the following command
firebase target:apply hosting myapp1 myapp1.mydomain-1100
firebase deploy --only hosting:myapp1
When i do that, cli throw me the next error Error: HTTP Error: 404, Requested entity was not found.
This is my firebase.json
{
"hosting": {
"target": "myapp1",
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
What i'm doing wrong? i search for hours and i dont found a solution for my case,
i really appreciate constructive answers.

Hosting rewrite with custom domain for function doesn't work

I am trying to setup firebase with a domain so that:
My functions are available on mydomain.com/api/functionName
My hosting is available on mydomain.com (or preferably mydomain.com/client/, but the other will work as well)
My firebase.json looks like this:
{
"hosting": {
"public": "build",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
},
{
"source": "/api/helloWorld",
"function": "helloWorld"
}
]
}
}
After deploying this and accessing the domain in the browser, both / and /api/helloWorld routes will always take my to my client app, not to my function.
What's really weird to me is that when running the emulator locally, hosting does not work at all but localhost:5000/api/helloWorld works as expected and calls the function.
What am I doing wrong here? It feels as if my firebase.json is not deployed at all.
Update
Here's my function definition if that has anything to do with it:
exports.helloWorld = functions
.region("europe-west3")
.https.onRequest((_, response) => {
response.send("Hello from Firebase!");
});
Rewrites work in a "first match wins" order. Since you have a ** at the top, it will match all URLs and the second rewrite is never accessed. If you switch the order, you should see it work as expected:
{
"hosting": {
// ...
"rewrites": [
{
"source": "/api/helloWorld",
"function": "helloWorld"
},
{
"source": "**",
"destination": "/index.html"
}
]
}
}

Issue with multiple sites deployment in Firebase

I have an Angular web application called "my-app".
I created a project on Google Firebase and called it "my-app" as well.
I deployed the Angular application to Firebase project "my-app". As a result, a site "my-app-*****" appeared. My app was accessible on "my-app-*****.web.app" address.
Then I created the second site in Hosting of the same project. I called it "my-app-dev". It got an address but was empty because nothing was deployed yet.
I followed instructions from google: https://firebase.google.com/docs/hosting/multisites
And even one question on StackOverflow: Firebase hosting deploy to other site
I added targets and modified firebase.json. In the result, they looked like that:
firebase.json file content:
"hosting": [
{
"target": "prod",
"public": "www",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
{
"target": "dev",
"public": "www",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
]
}
.firebaserc file content:
{
"projects": {
"default": "my-app-*****"
},
"targets": {
"my-app-*****": {
"hosting": {
"dev": [
"my-app-dev"
],
"prod": [
"my-app-*****"
]
}
}
}
}
But I faced an error when I executed command firebase deploy --only hosting:dev
However, when I executed command firebase deploy --only hosting:prod, everything was deployed without errors.
As a result, I am able to deploy my project to "my-app-*****" site, but I could not deploy my project to "my-app-dev" site. I want both sites to use same built files, that is why "public" was pointing to the same directory "www". Both sites located in the same Hosting of "my-app" project on Google Firebase.
Can someone please help me and explain what have I done wrong?

Firebase Hosting proxy to Cloud Functions only triggers in us-central1

I have my firebase cloud functions created with region: asia-northeast1, I am trying to trigger one of the functions using the firebase.json file.
"hosting": [
{
"target": "ksite",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{ "source": "**", "function": "ssr" }
]
}
]
Unfortunately this triggers the function with region of us-central1 https://us-central1-site.cloudfunctions.net/ssr/ instead of asia-northeast1 something like this https://asia-northeast1-site.cloudfunctions.net/ssr/
How and why is this happening?
Here is my function:
// For Universal Rendering
export const ssr = functions.region('asia-northeast1').https.onRequest((request, response) => {
require(`${process.cwd()}/dist/ksite-webpack/server`).app(request, response);
});
Firebase Hosting currently only supports the proxy of URLs to functions deployed to us-central1, as mentioned in the documentation:
Important: Firebase Hosting supports Cloud Functions in us-central1 only.
And also here in the docs:
Important: If you are using HTTP functions to serve dynamic content for Firebase Hosting, you must use us-central1.

Firebase Hosting rewrites set up to pass all other URL to a particular Cloud Functions

I have a NextJS app and an express.js API both hosted in Firebase Cloud Functions.
This is my firebase.json
{
"hosting": {
"public": "src/public",
"rewrites": [
{
"source": "/user",
"function": "api"
},
{
"source": "/users",
"function": "api"
},
{
"source": "**/**",
"function": "next"
}
]
},
"functions": {
"source": "src/functions"
}
}
What I expecting was to rewrite /user and /users to api Cloud Function and all other URL to next Cloud Function. But it not working as I expected. Every URL is passing to next Cloud Function even /user and /users.
And I need help...
Try the below code, you can find its used on an example of the documentation that is intended for requests to files or directories that do not exist, including all other replies I understand
// Serves index.html for requests to files or directories that do not exist
"source": "**",
"function": "next"

Resources