0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Updated code for fetching location (#21368)

Ref:
https://linear.app/ghost/issue/ENG-1660/undefined-location-when-logging-in-on-ios
This commit is contained in:
Princi Vershwal 2024-10-23 06:43:42 +01:00 committed by GitHub
parent 1e8bb253bf
commit 3f1fa96003
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -162,11 +162,11 @@ module.exports = function createSessionService({
/**
* Get a readable location string from an IP address.
* @param {string} ip - The IP address to look up.
* @returns {Promise<string>} - A readable location string or 'Unknown Location'.
* @returns {Promise<string>} - A readable location string or 'Unknown'.
*/
async function getGeolocationFromIP(ip) {
if (!ip || (!IPV4_REGEX.test(ip) && !IPV6_REGEX.test(ip))) {
return;
return 'Unknown';
}
const gotOpts = {
@ -178,27 +178,20 @@ module.exports = function createSessionService({
}
const geojsUrl = `https://get.geojs.io/v1/ip/geo/${encodeURIComponent(ip)}.json`;
const response = await got(geojsUrl, gotOpts).json();
const {
city = '',
region = '',
country = ''
} = response || {};
try {
const response = await got(geojsUrl, gotOpts).json();
const locationParts = [];
const {city, region, country} = response || {};
if (city) {
locationParts.push(city);
// Only include non-empty parts in the result
const locationParts = [city, region, country].filter(Boolean);
// If no valid parts, return 'Unknown'
return locationParts.length > 0 ? locationParts.join(', ').trim() : 'Unknown';
} catch (error) {
return 'Unknown';
}
if (region) {
locationParts.push(region);
}
if (country) {
locationParts.push(country);
}
return locationParts.join(', ').trim() || 'Unknown Location';
}
async function getDeviceDetails(userAgent, ip) {