feat: add client info in backend fetch header

This commit is contained in:
2026-01-20 08:25:10 +07:00
parent cb436fe40c
commit e27b18b22e
5 changed files with 37 additions and 3 deletions

View File

@ -1,4 +1,7 @@
"use server";
import { headers } from "next/headers";
import { UAParser } from "ua-parser-js";
export interface BackendResponse<T = unknown> {
success: boolean;
message: string;
@ -7,10 +10,23 @@ export interface BackendResponse<T = unknown> {
}
export const backendFetch = async (path: string, options: RequestInit = {}) => {
const userAgent = (await headers()).get("user-agent") || "";
const userIp = (await headers()).get("x-forwarded-for") || "unknown";
const ua = new UAParser(userAgent).getResult();
const clientInfo = {
os: ua.os.name ?? "unknown",
osVersion: ua.os.version ?? "unknown",
browser: ua.browser.name ?? "unknown",
browserVersion: ua.browser.version ?? "unknown",
deviceType: ua.device.type ?? "desktop",
ip: userIp,
};
const res = await fetch(`${process.env.BACKEND_ENDPOINT}/${path}`, {
...options,
headers: {
"Content-Type": "application/json",
"x-client-info": JSON.stringify(clientInfo),
Authorization: `Bearer ${process.env.BACKEND_API_KEY}`,
...options.headers,
},

View File

@ -0,0 +1,10 @@
import { cookies } from "next/headers";
import React from "react";
const AuthSessionProvider = ({children}: readonly<{children: React.ReactNode}>) => {
const cookieHeader = cookies().toString();
console.log("Cookies in AuthSessionProvider:", cookieHeader);
return <AuthContext.Provider value={{ cookie = cookieHeader }}>{children}</AuthContext.Provider>;
};
export default AuthSessionProvider;

View File

@ -2,8 +2,6 @@
import Image from "next/image";
import NavigationLink from "./NavigationLink";
import SignIn from "./SignIn";
import { Dialog, DialogTrigger } from "@/shared/libs/shadcn/ui/dialog";
import PopupWrapper from "../../signin/components/PopupWrapper";
const Navbar = () => {
return (