I'm trying to get the IP address from the client in the firebase function by using javascript . Why do I need IP? To make whitelist IPs that can access my functions.
my goal: access my function by IP address
I tried to use this method but didn't work for me it give me 'undefined' :
exports.getIP = functions.https.onRequest(async (req, res) => {
var call = req.ip
console.log(call);
res.send(call);
});
exports.getIP = functions.https.onRequest(async (req, res) => {
var call = req.headers['x-forwarded-for']
console.log(call);
res.send(call);
});
Here when I call req.headers :
Both the 'x-forwarded-for' and x-appengine-user-ip headers will be added when you deploy your Cloud functions. It seems you are using Firebase Functions emulator which runs on localhost (since the host header is localhost:5001). You can try deploying the function and logging the IP.
The
X-Forwarded-For(XFF) header is a de-facto standard header for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or a load balancer.
The
X-Appengine-User-IPis set by App Engine.
When you emulate functions locally there is no proxy in between hence those headers are missing.
References: