Skip to main content

Overview

The getGeolocationData() utility function retrieves the user’s current geolocation coordinates.

Import

import { getGeolocationData } from "magenx404/utils";

Usage

import { getGeolocationData } from "magenx404/utils";

const location = await getGeolocationData();
console.log(location);
// {
//   latitude: 37.7749,
//   longitude: -122.4194,
//   error: null,
//   isFetching: false
// }

Return Value

latitude
number | null
User’s latitude coordinate
longitude
number | null
User’s longitude coordinate
error
string | null
Error message if geolocation access failed or was denied
isFetching
boolean
Whether geolocation is currently being fetched

Example

"use client";

import { getGeolocationData } from "magenx404/utils";
import { X404Blacklist } from "magenx404";

export async function authenticateWithLocation() {
  // Get user's location
  const location = await getGeolocationData();

  if (location.error) {
    console.error("Geolocation error:", location.error);
    // Continue without location or handle error
  }

  // Use location in authentication
  const result = await X404Blacklist({
    excluded_mints: ["token_address"],
    max_holdings: {},
    geo_code: location.latitude ? "true" : "false",
    geo_code_locs: "",
    coords: {
      latitude: location.latitude,
      longitude: location.longitude,
    },
  });

  return result;
}

Error Handling

The function handles various geolocation scenarios:
  • Permission denied: User denied location access
  • Timeout: Location request timed out
  • Not available: Geolocation API not available
  • Position unavailable: Could not determine position
const location = await getGeolocationData();

if (location.error) {
  switch (location.error) {
    case "User denied Geolocation":
      console.log("User denied location access");
      break;
    case "Position unavailable":
      console.log("Could not determine position");
      break;
    default:
      console.log("Geolocation error:", location.error);
  }
}
The browser will prompt the user for location permission on first use. Make sure to handle cases where the user denies permission.