From 3f1fa9600324826bb8915c6ffc60bcd0a0d1efc0 Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Wed, 23 Oct 2024 06:43:42 +0100 Subject: [PATCH] Updated code for fetching location (#21368) Ref: https://linear.app/ghost/issue/ENG-1660/undefined-location-when-logging-in-on-ios --- ghost/session-service/lib/session-service.js | 31 ++++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/ghost/session-service/lib/session-service.js b/ghost/session-service/lib/session-service.js index 8dd02e8337..4d4166829c 100644 --- a/ghost/session-service/lib/session-service.js +++ b/ghost/session-service/lib/session-service.js @@ -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} - A readable location string or 'Unknown Location'. + * @returns {Promise} - 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) {