overcentric
Version:
A lightweight, privacy-focused toolkit for modern SaaS web applications
42 lines (41 loc) • 1.6 kB
JavaScript
import { v4 as uuidv4 } from 'uuid';
const isBrowser = () => typeof window !== 'undefined';
export function generateUuid() {
return uuidv4();
}
export function storeDeviceId(deviceId) {
if (!isBrowser())
return;
try {
// First, set the cookie on the exact current domain (no domain attribute)
// This ensures it works on the current domain regardless of subdomain structure
document.cookie = `overcentric_device_id=${deviceId}; path=/; max-age=31536000; SameSite=Strict; Secure`;
// Then try to also set it at the parent domain level as before
try {
const domain = window.location.hostname.split('.').slice(-2).join('.');
document.cookie = `overcentric_device_id=${deviceId}; domain=.${domain}; path=/; max-age=31536000; SameSite=Strict; Secure`;
}
catch (e) {
// If setting on parent domain fails, we already have the cookie on current domain
console.debug('Could not set cookie on parent domain');
}
}
catch (e) {
console.warn('Failed to store device ID:', e);
}
}
export function getDeviceId() {
var _a, _b;
if (!isBrowser())
return null;
try {
const deviceId = (_b = (_a = document.cookie
.split('; ')
.find(row => row.startsWith('overcentric_device_id='))) === null || _a === void 0 ? void 0 : _a.split('=')) === null || _b === void 0 ? void 0 : _b[1];
return deviceId || null;
}
catch (e) {
console.warn('Failed to get device ID:', e);
return null;
}
}