Files
var-monorepo/apps/hub/app/(auth)/login/_components/action.ts
2026-01-30 00:25:51 +01:00

54 lines
1.1 KiB
TypeScript

"use server";
import { LOG_TYPE, prisma } from "@repo/db";
import { getServerSession } from "api/auth/[...nextauth]/auth";
import { randomUUID } from "crypto";
import { cookies, headers } from "next/headers";
export async function getOrSetDeviceId() {
const store = await cookies();
let deviceId = store.get("device_id")?.value;
if (!deviceId) {
deviceId = randomUUID();
store.set("device_id", deviceId, {
httpOnly: true,
secure: true,
sameSite: "lax",
path: "/",
maxAge: 60 * 60 * 24 * 365, // 1 Jahr
});
}
return deviceId;
}
export const logAction = async (
type: LOG_TYPE,
otherValues?: {
field: string;
oldValue: string;
newValue: string;
},
) => {
const headersList = await headers();
const user = await getServerSession();
console.log("headers");
const deviceId = await getOrSetDeviceId();
await prisma.log.create({
data: {
type,
browser: headersList.get("user-agent") || "unknown",
userId: user?.user.id,
deviceId: deviceId,
ip:
headersList.get("X-Forwarded-For") ||
headersList.get("Forwarded") ||
headersList.get("X-Real-IP"),
...otherValues,
},
});
};