mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -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:
parent
1e8bb253bf
commit
3f1fa96003
1 changed files with 12 additions and 19 deletions
|
@ -162,11 +162,11 @@ module.exports = function createSessionService({
|
||||||
/**
|
/**
|
||||||
* Get a readable location string from an IP address.
|
* Get a readable location string from an IP address.
|
||||||
* @param {string} ip - The IP address to look up.
|
* @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) {
|
async function getGeolocationFromIP(ip) {
|
||||||
if (!ip || (!IPV4_REGEX.test(ip) && !IPV6_REGEX.test(ip))) {
|
if (!ip || (!IPV4_REGEX.test(ip) && !IPV6_REGEX.test(ip))) {
|
||||||
return;
|
return 'Unknown';
|
||||||
}
|
}
|
||||||
|
|
||||||
const gotOpts = {
|
const gotOpts = {
|
||||||
|
@ -178,27 +178,20 @@ module.exports = function createSessionService({
|
||||||
}
|
}
|
||||||
|
|
||||||
const geojsUrl = `https://get.geojs.io/v1/ip/geo/${encodeURIComponent(ip)}.json`;
|
const geojsUrl = `https://get.geojs.io/v1/ip/geo/${encodeURIComponent(ip)}.json`;
|
||||||
const response = await got(geojsUrl, gotOpts).json();
|
|
||||||
|
|
||||||
const {
|
try {
|
||||||
city = '',
|
const response = await got(geojsUrl, gotOpts).json();
|
||||||
region = '',
|
|
||||||
country = ''
|
|
||||||
} = response || {};
|
|
||||||
|
|
||||||
const locationParts = [];
|
const {city, region, country} = response || {};
|
||||||
|
|
||||||
if (city) {
|
// Only include non-empty parts in the result
|
||||||
locationParts.push(city);
|
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) {
|
async function getDeviceDetails(userAgent, ip) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue