#GeoIP-lite with nuxt3
1 messages · Page 1 of 1 (latest)
It looks like geoip-lite is built for Node and not for browser?
Ok, i create a server api but mu app its on vercel and its no getting the true user ip.
https://vercel.com/docs/concepts/edge-network/headers
Would you not need to read these from Vercel?
x-real-ip or x-forwarded-for
im using this but return the current ip from where my app is
@abstract forum
export default function useUserAgent() {
if (process.server) {
const nuxtApp = useNuxtApp()
if (nuxtApp.ssrContext && nuxtApp.ssrContext.event.node.req) {
const ip =
nuxtApp.ssrContext.event.node.req.headers["x-forwarded-for"] ||
nuxtApp.ssrContext.event.node.req.connection.remoteAddress || // for local dev
nuxtApp.ssrContext.event.node.req.socket.remoteAddress;
console.log("realip", nuxtApp.ssrContext.event.node.req.headers["x-real-ip"]);
console.log("forwardedfor", nuxtApp.ssrContext.event.node.req.headers["x-forwarded-for"]);
console.log("vercelforwardedfor", nuxtApp.ssrContext.event.node.req.headers["x-vercel-forwarded-for"]);
console.log("x-vercel-ip-country", nuxtApp.ssrContext.event.node.req.headers["x-vercel-ip-country"]);
const ua = nuxtApp.ssrContext.event.node.req.headers["user-agent"];
const device = setDevice(ua)
return {
userAgent: ua || "",
ip: typeof ip === "string" ? ip : undefined,
device
};
}
}
return {
userAgent: ua,
ip: undefined,
device
};
}``` my composable
Does this work
?
The vercel is returning the ip apparently where the application is. If I access it by using a VPN to test it, it always returns to the country where the application is.
are you doing this on server side?
i was doing it over .vue but ive tested it on server/api/status with ```js
export default defineEventHandler(async (event) => {
const ip =
event.node.req.headers["x-forwarded-for"] ||
event.node.req.connection.remoteAddress || // for local dev
event.node.req.socket.remoteAddress;
return {
realip: event.node.req.headers['x-real-ip'] || event.node.req.headers['x-forwarded-for'] || event.node.ip,
forwardedfor: event.node.req.headers['x-forwarded-for'],
vercelforwardedfor: event.node.req.headers['x-vercel-forwarded-for'],
'x-forwarded-for': event.node.req.headers['x-forwarded-for'],
remoteAddress: event.node.req.connection.remoteAddress, // for local dev
remoteAddress: event.node.req.socket.remoteAddress,
'x-real-ip': event.node.req.headers['x-real-ip'],
'x-forwarded-for': event.node.req.headers['x-forwarded-for'],
'x-vercel-forwarded-for': event.node.req.headers['x-vercel-forwarded-for'],
};
});
Yeah I guess this won’t work on server side.
Read the last line
just Enterprise only so
Could it be that if I create an api and call it, I will still receive the request with the vercel ip?
could be worth a try atleast.
@abstract forum
bro, I created a basic serverless here and when accessing with vercel, it takes another address
So nuxt nitro is counted as a proxy then 🤔
i dont know, very bizarre, if I access my endpoint through the browser (from the serverless) that I created, it takes the correct one, but if I call it either in the application directly in the route or in the project's api, it returns the address where the serverless it is
Is there any configuration I can test?
i saw this @abstract forum , i dont know if this has to do with my problem
Hm not sure if i'm being honest
🥹
Maybe try https://vercel.com/docs/concepts/projects/project-configuration#headers
Can be the solution.
Other then that i would probably ask Vercel if there is something that needs to be fixed on their end etc.
@woeful grotto Any success?
yes