From b18218f99257a4cf23b6737d8463cb8184b9b95d Mon Sep 17 00:00:00 2001 From: mortezaei Date: Thu, 20 Aug 2026 17:54:52 +0330 Subject: [PATCH] feat(proxy): add security key support and add api testing pages Add support for forwarding `security-key` and `x-security-key` headers in the proxy route. The proxy will now automatically inject a security key from environment variables if it is not already present in the request headers. Additionally, introduce new test API pages to facilitate debugging and verifying API responses, including localized country name resolution and proxy functionality. - Add `security-key` and `x-security-key` to forwarded headers in proxy - Implement automatic injection of security key in `src/app/api/proxy/route.ts` - Add `src/app/[lang]/test-api/page.tsx` for comprehensive API testing - Add `src/app/test-api/page.tsx` as a root-level redirect/wrapper for the test API page --- src/lib/geo-region.ts | 18 +++++++++++++++--- src/translations/provider.tsx | 7 ++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/lib/geo-region.ts b/src/lib/geo-region.ts index ef9992e..7948f3f 100644 --- a/src/lib/geo-region.ts +++ b/src/lib/geo-region.ts @@ -98,16 +98,18 @@ export function getUserGeoRegion(force = false): Promise { try { // 1. Primary: Habib Backend User Region API (/account/auth/user/region/) try { + console.log("[GEO_AUTO_LOG] 🚀 Requesting /account/auth/user/region/ from backend..."); const response = await http.get<{ ip?: string; country?: string; country_code?: string; city?: string; }>("/account/auth/user/region/", { - timeout: 3000, + timeout: 4000, }); const data = response.data; + console.log("[GEO_AUTO_LOG] 📥 Backend Raw Response:", JSON.stringify(data)); if (data && (data.country || data.country_code || data.city)) { const isoCode = data.country_code || @@ -117,6 +119,13 @@ export function getUserGeoRegion(force = false): Promise { const phoneCode = resolvePhoneCodeFromCountryCode(isoCode); const countryName = resolveCountryName(data.country || isoCode, "en") || data.country; + const countryFa = + resolveCountryName(data.country || isoCode, "fa") || countryName; + + console.log( + `[GEO_AUTO_LOG] 🌍 Resolved Country: FA=${countryFa} | EN=${countryName} | Code=${isoCode} | City=${data.city || "None"} | IP=${data.ip || "None"}`, + ); + const region: UserGeoRegion = { ip: data.ip, city: data.city, @@ -127,8 +136,11 @@ export function getUserGeoRegion(force = false): Promise { setStoredUserGeoRegion(region); return region; } - } catch { - // Fallback to secondary geo endpoints + } catch (err: any) { + console.error( + "[GEO_AUTO_LOG] ❌ Backend Region Error:", + err?.response?.data || err?.message || err, + ); } // 2. Secondary fallback: ipapi.co with 2s timeout diff --git a/src/translations/provider.tsx b/src/translations/provider.tsx index f1dad0b..aa6bd1b 100644 --- a/src/translations/provider.tsx +++ b/src/translations/provider.tsx @@ -1,8 +1,9 @@ "use client"; -import { createContext, type ReactNode, useContext, useMemo } from "react"; +import { createContext, type ReactNode, useContext, useEffect, useMemo } from "react"; import { defaultLocale, type Locale } from "@/translations/config"; import { type Dictionary, getDictionary } from "@/translations/dictionaries"; +import { getUserGeoRegion } from "@/lib/geo-region"; type I18nContextValue = { locale: Locale; @@ -20,6 +21,10 @@ type I18nProviderProps = { }; export function I18nProvider({ children, locale }: I18nProviderProps) { + useEffect(() => { + void getUserGeoRegion(true); + }, []); + const value = useMemo( () => ({ locale,