Next.js Discord

Discord Forum

getting IP from client

Unanswered
Alaskan Malamute posted this in #help-forum
Open in Discord
Alaskan MalamuteOP
Any way of getting it on server side component? I found this solution but is there any other without relying on third party API?
fetch('https://api.ipify.org?format=json')
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.error('Error fetching IP:', error);
});

3 Replies

Alaskan MalamuteOP
I also thought about using this thing but suddenly there're many vulnerabilities in it
 const ip = headersList.get('x-forwarded-for').split(/, /)[0];
you mean spoofing? yes you can trust the last one after removing secured reverse proxy
raw ip is the direct client so usually reverse proxy or vpn ip