Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user IP address in Vue.js

Tags:

vue.js

ip

I'm new in Vue.js. I need to get user ip address inside Vue.js.

What i have to do before is req.ip inside API, but my API always get Vue.js server IP.

So i think Vue.js that must determine what user ip is. But i still not find a best way to get user ip inside Vue.js. Anyone help ?

like image 692
Crocodile Avatar asked Sep 09 '18 10:09

Crocodile


1 Answers

You need to send a request to expose the clients IP to some API. For instance you can do the following request with only vanilla JS (in mounted() for example) and then save the IP to data.

fetch('https://api.ipify.org?format=json')
  .then(response => response.json())
  .then(response => {
    this.clientIp = response.ip;
  });
like image 97
perlindstroem Avatar answered Nov 05 '22 04:11

perlindstroem