21 lines
495 B
TypeScript
21 lines
495 B
TypeScript
import { v4 as uuidv4 } from "uuid";
|
|
|
|
const DEVICE_ID_KEY = "x-device-id";
|
|
|
|
export function getOrCreateDeviceId(): string {
|
|
if (typeof window === "undefined") return "";
|
|
|
|
const existing = localStorage.getItem(DEVICE_ID_KEY);
|
|
if (existing) return existing;
|
|
|
|
const id = uuidv4();
|
|
localStorage.setItem(DEVICE_ID_KEY, id);
|
|
return id;
|
|
}
|
|
|
|
export function clearDeviceId(): void {
|
|
if (typeof window === "undefined") return;
|
|
|
|
localStorage.removeItem(DEVICE_ID_KEY);
|
|
}
|