add userSession module

This commit is contained in:
rafiarrafif
2025-05-08 15:21:05 +07:00
parent cb7757a83b
commit a2c27b313a
11 changed files with 125 additions and 1 deletions

View File

@ -0,0 +1,26 @@
import { Context } from "elysia";
import { UAParser } from "ua-parser-js";
import { UserHeaderInformation } from "./types";
export const getUserHeaderInformation = (
ctx: Context
): UserHeaderInformation => {
const headers = ctx.request.headers;
const userAgentHeader = headers.get("user-agent") || "desktop";
const userAgent = new UAParser(userAgentHeader);
const userIP =
headers.get("cf-connecting-ip") ||
headers.get("x-real-ip") ||
headers.get("x-forwarded-for")?.split(",")[0] ||
"Unknown IP";
const userHeaderInformation = {
ip: userIP,
deviceType: userAgent.getDevice().type || "desktop",
deviceOS: userAgent.getOS().name + " " + userAgent.getOS().version,
browser: userAgent.getBrowser().name + " " + userAgent.getBrowser().version,
};
return userHeaderInformation;
};

View File

@ -0,0 +1,6 @@
export interface UserHeaderInformation {
ip: string;
deviceType: string;
deviceOS: string;
browser: string;
}