many changes
This commit is contained in:
@@ -1,30 +1,26 @@
|
||||
/**
|
||||
* Device ID Management Service
|
||||
*
|
||||
* Manages device identification securely for Jellyfin server communication.
|
||||
* Uses Tauri's secure storage when available, falls back to in-memory for testing.
|
||||
* Manages device identification for Jellyfin server communication.
|
||||
* The Rust backend handles UUID generation and persistent storage in the database.
|
||||
* This service provides a simple interface with in-memory caching.
|
||||
*
|
||||
* TRACES: UR-009 | DR-011
|
||||
*/
|
||||
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
let cachedDeviceId: string | null = null;
|
||||
|
||||
/**
|
||||
* Generate a UUID v4 for device identification
|
||||
*/
|
||||
function generateUUID(): string {
|
||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
||||
const r = (Math.random() * 16) | 0;
|
||||
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the device ID.
|
||||
* Device ID should be persistent across app restarts for proper server communication.
|
||||
* Device ID is a UUID v4 that persists across app restarts.
|
||||
* On first call, the Rust backend generates and stores a new UUID.
|
||||
* On subsequent calls, the stored UUID is retrieved.
|
||||
*
|
||||
* @returns The device ID string
|
||||
* @returns The device ID string (UUID v4)
|
||||
*
|
||||
* TRACES: UR-009 | DR-011
|
||||
*/
|
||||
export async function getDeviceId(): Promise<string> {
|
||||
// Return cached value if available
|
||||
@@ -33,40 +29,21 @@ export async function getDeviceId(): Promise<string> {
|
||||
}
|
||||
|
||||
try {
|
||||
// Try to get from Tauri secure storage (Rust backend manages this)
|
||||
const deviceId = await invoke<string | null>("device_get_id");
|
||||
|
||||
if (deviceId) {
|
||||
cachedDeviceId = deviceId;
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
// If no device ID exists, generate and store a new one
|
||||
const newDeviceId = generateUUID();
|
||||
try {
|
||||
await invoke("device_set_id", { deviceId: newDeviceId });
|
||||
} catch (e) {
|
||||
console.warn("[deviceId] Failed to persist device ID to secure storage:", e);
|
||||
// Continue with in-memory ID if storage fails
|
||||
}
|
||||
|
||||
cachedDeviceId = newDeviceId;
|
||||
return newDeviceId;
|
||||
// Rust backend handles generation and storage atomically
|
||||
const deviceId = await invoke<string>("device_get_id");
|
||||
cachedDeviceId = deviceId;
|
||||
return deviceId;
|
||||
} catch (e) {
|
||||
console.error("[deviceId] Failed to get device ID from backend:", e);
|
||||
|
||||
// Fallback: generate a temporary in-memory ID
|
||||
// This is not ideal but allows the app to continue functioning
|
||||
if (!cachedDeviceId) {
|
||||
cachedDeviceId = generateUUID();
|
||||
}
|
||||
return cachedDeviceId;
|
||||
throw new Error("Failed to initialize device ID: " + String(e));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached device ID synchronously (if available)
|
||||
* This should be used after initial getDeviceId() call
|
||||
* This should only be used after initial getDeviceId() call
|
||||
*
|
||||
* @returns The cached device ID, or empty string if not yet initialized
|
||||
*/
|
||||
export function getDeviceIdSync(): string {
|
||||
return cachedDeviceId || "";
|
||||
|
||||
Reference in New Issue
Block a user