user3255364
user3255364

Reputation: 46

Get IPv4 Address in Angular using typescript

I am looking for a way to get IPv4 Address in Angular 7 or greater. I need to identify user that has opened an app in browser by local IP address since the public IP is usually the same for all users in one company.

I tried this example here on the link and some more, but no success so far. Do you have any idea for me? Is it possible at all?

Thanks in advance!

Upvotes: 1

Views: 1963

Answers (1)

Ali Habibzadeh
Ali Habibzadeh

Reputation: 11568

You can't do that with ng only. You need access to the request object of page angular is loaded on. For that you need to write some server code.

If you're on node.js try using geoip-lite and then write a GET endpoint that uses the x-forwarded-for header and as fallback uses the requester's ip via req.connection.remoteAddress.split(":")[3]

Then you can return that results to the ng app via

const ip = xForwadedFor || remoteIP;
const geo = geoip.lookup(ip);

Then you need to consume the results from the GET endpoint through the http service of ng.

Upvotes: 2

Related Questions