Skip to content

Commit

Permalink
Fixed: Removed IP parameter from ipinfo.io request
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Jun 11, 2024
1 parent b174bb8 commit 9eff5d9
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/logs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ async function logUserActivity(userlog: userlogType, req: Request) {
try {
const logs_collection = db.collection("user_logs");

const ip_address = (req.headers.get("x-forwarded-for") ?? "127.0.0.1")
.split(",")[0]
.trim();

const user_log = {
username: userlog.username,
name: userlog.name,
Expand All @@ -29,8 +25,10 @@ async function logUserActivity(userlog: userlogType, req: Request) {
userType: userlog.role,
timing: new Date().toISOString(),
device: req.headers.get("user-agent") || "",
ip: ip_address,
location: await fetchLocationByIP(ip_address),
ip: (req.headers.get("x-forwarded-for") ?? "127.0.0.1")
.split(",")[0]
.trim(),
location: await fetchLocationByIP(),
};

//stores in user_logs
Expand All @@ -50,9 +48,10 @@ async function logUserActivity(userlog: userlogType, req: Request) {
}
}

async function fetchLocationByIP(ip: string) {
async function fetchLocationByIP() {
// No need to pass IP as it will be automatically detected by ipinfo.io
const request = await fetch(
`https://ipinfo.io/${ip}/json?token=${process.env.IP_INFO_TOKEN}`
`https://ipinfo.io/json?token=${process.env.IP_INFO_TOKEN}`
);
const jsonResponse = await request.json();
const location = `${jsonResponse.city}, ${jsonResponse.region}, ${jsonResponse.country}`;
Expand Down

0 comments on commit 9eff5d9

Please sign in to comment.