1

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 :

enter image description here

Khalifa Alkhatri
  • 244
  • 1
  • 4
  • 20
  • 1
    `x-forwarded-for` is a valid header and seems to be working. You can also try using `x-appengine-user-ip` header. At worst, can you `console.log(req.headers)` and share the output? – Dharmaraj Aug 28 '21 at 13:42
  • @Dharmaraj I updated question, I tried to run 'x-appengine-user-ip' but it give me undefined. – Khalifa Alkhatri Aug 28 '21 at 13:48

1 Answers1

4

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-IP is set by App Engine.

When you emulate functions locally there is no proxy in between hence those headers are missing.

References:

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84