I'm having some trouble with serving some dynamic content from Firebase Hosting.
I've written an http.onRequest()
cloud function that returns an image (content-type: image/jpeg) as its response. The function works as expected if I access it directly at its url:
https://us-central1-my-project-id.cloudfunctions.net/hosting-getPartnerImg
Per the documentation, I am using the us-central1
region.
I would like to be able to invoke this function using Firebase Hosting as well, which I've configured as follows:
firebase.json (snippet)
"rewrites" : [
{
"source" : "/pimg",
"function" : "hosting-getPartnerImage"
}
],
"headers": [
{
"source": "/pimg",
"headers": [ {
"key": "Cache-Control",
"value": "max-age=60"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
}]
index.js (snippet)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
let firebaseDefaultConfig = JSON.parse(process.env.FIREBASE_CONFIG);
admin.initializeApp(firebaseDefaultConfig);
const fn = process.env.FUNCTION_NAME;
if(!fn || fn === 'hosting-getPartnerImg'){
exports.hosting = require('./hosting.js');
}
hosting.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.getPartnerImg = functions.region("us-central1").https.onRequest((req, res) => {
const partnerId = req.query.partner;
const fileName = req.query.file;
res.set("content-type", "image/jpeg");
const bucket = admin.storage().bucket();
let file = bucket.file("partnerImgs/" + partnerId + "/" + fileName);
let readStream = file.createReadStream();
readStream.pipe(res);
});
This one has me stumped. Navigating to a URL like:
https://my-project-id.web.app/pimg?partner=BLAH&file=foo.jpg
does not generate a Page Not Found as other URLs, so I'm reasonably confident that the rewrite is taking hold as it should. Be that the case though, why am I immediately taken to:
appengine.google.com/_ah/loginform...
with the message:
** An application is requesting permission to your Google Account **
Can't HTTP onRequest
cloud functions be used anonymously via Firebase Hosting? Why does the function work when I hit it directly, yet requests permission when I access it via the Firebase Hosting rewrite.
Any ideas would be appreciated.
The AppEngine login page behavior is what happens when the function being called doesn't exist (regardless of if it is via a rewrite or not).
Your problem is this rewrite:
"function" : "hosting-getPartnerImage"
vs the actual function name:
if(!fn || fn === 'hosting-getPartnerImg'){
or
exports.getPartnerImg = ...
Notably, the rewrite doesn't call the correct function.
You should change the rewrite to call hosting-getPartnerImg
instead.