GeoIP-lite with nuxt3

Im trying to use geoip-lite, but im getting this erro, what could be?
No description
23 Replies
conner
conner2y ago
It looks like geoip-lite is built for Node and not for browser?
formigueiro
formigueiroOP2y ago
Ok, i create a server api but mu app its on vercel and its no getting the true user ip.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
im using this but return the current ip from where my app is @SeatloN
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
};
}
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
conner
conner2y ago
Does this work ?
formigueiro
formigueiroOP2y ago
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.
conner
conner2y ago
are you doing this on server side?
formigueiro
formigueiroOP2y ago
i was doing it over .vue but ive tested it on server/api/status with
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'],
};
});
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'],
};
});
its not returning the truth ip
conner
conner2y ago
Yeah I guess this won’t work on server side.
formigueiro
formigueiroOP2y ago
yeah but neather over server api looks like vercel its hidden client ip
conner
conner2y ago
Vercel Documentation
Headers Included with Deployments
This reference covers the list of request, response, cache-control, and custom response headers included with deployments with Vercel.
conner
conner2y ago
Read the last line
formigueiro
formigueiroOP2y ago
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?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
@SeatloN bro, I created a basic serverless here and when accessing with vercel, it takes another address
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
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?
formigueiro
formigueiroOP2y ago
i saw this @SeatloN , i dont know if this has to do with my problem
No description
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
formigueiro
formigueiroOP2y ago
🥹
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
conner
conner2y ago
@formigueiro Any success?
formigueiro
formigueiroOP2y ago
yes i had to use vercel headers to get country
Want results from more Discord servers?
Add your server