how to transfer the client IP with SSR to the backend? Now I am transmitting the IP of the server on which the project is located
Global fetch configuration
import { ofetch } from 'ofetch';
import { defineNuxtPlugin } from '#app';
export default defineNuxtPlugin(() => {
const headers = useRequestHeaders(['cookie']);
globalThis.$fetch = ofetch.create({
headers,
credentials: 'include',
retry: 0,
});
});
Plugin for initializing a user session and obtaining his geo
geo.ts
import { useGeoStore } from '~/store/geo';
export default defineNuxtPlugin(async () => {
const geoStore = useGeoStore();
const { data: geo } = await useFetch('/api/v1/session', {
baseURL: useRuntimeConfig().public.API_BASE_URL,
headers: useRequestHeaders(['cookie']),
watch: false
});
});
Made a proxy layer for api requests
server/api/[…].ts
import { joinURL } from 'ufo';
import { defineEventHandler } from 'h3';
export default defineEventHandler((event) => {
const config = useRuntimeConfig();
return proxyRequest(event, joinURL(config.public.API_BASE_URL, event.path));
});
if I rename geo.ts and geo.client.ts then everything works correctly and the IP is transferred to the user