✨ feat: add client info in backend fetch header
This commit is contained in:
@ -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,
|
||||
},
|
||||
|
||||
10
shared/providers/AuthSession.tsx
Normal file
10
shared/providers/AuthSession.tsx
Normal 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;
|
||||
@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user