| |
| |
| |
| |
|
|
| const DEVICE_ID_KEY = 'metarec_device_id' |
|
|
| |
| |
| |
| function generateDeviceId(): string { |
| |
| const timestamp = Date.now() |
| const random = Math.random().toString(36).substring(2, 15) |
| return `device_${timestamp}_${random}` |
| } |
|
|
| |
| |
| |
| |
| |
| export function getDeviceId(): string { |
| try { |
| |
| let deviceId = localStorage.getItem(DEVICE_ID_KEY) |
| |
| if (!deviceId) { |
| |
| deviceId = generateDeviceId() |
| localStorage.setItem(DEVICE_ID_KEY, deviceId) |
| } |
| |
| return deviceId |
| } catch (error) { |
| |
| console.warn('localStorage not available, using sessionStorage:', error) |
| try { |
| let deviceId = sessionStorage.getItem(DEVICE_ID_KEY) |
| |
| if (!deviceId) { |
| deviceId = generateDeviceId() |
| sessionStorage.setItem(DEVICE_ID_KEY, deviceId) |
| } |
| |
| return deviceId |
| } catch (e) { |
| |
| console.warn('sessionStorage not available, using temporary ID:', e) |
| return generateDeviceId() |
| } |
| } |
| } |
|
|
| |
| |
| |
| export function clearDeviceId(): void { |
| try { |
| localStorage.removeItem(DEVICE_ID_KEY) |
| sessionStorage.removeItem(DEVICE_ID_KEY) |
| } catch (error) { |
| console.warn('Failed to clear device ID:', error) |
| } |
| } |
|
|
|
|