-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookies.js
More file actions
57 lines (50 loc) · 1.65 KB
/
Copy pathcookies.js
File metadata and controls
57 lines (50 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Storage & Cookie Management Utility
* Handles authentication tokens (cookies) and UI/App state (localStorage).
*/
// --- Cookie Management (For User Logins & Auth) ---
function setCookie(name, value, days) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
// Secure, Lax SameSite cookie configuration keeps auth safe
document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Lax; Secure";
}
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function deleteCookie(name) {
// Setting an expiration date in the past deletes the cookie
setCookie(name, "", -1);
}
// --- Local Storage Management (For UI Changes & App Data) ---
function saveLocalData(storageKey, dataObject) {
try {
localStorage.setItem(storageKey, JSON.stringify(dataObject));
return true;
} catch (e) {
console.error(`Failed to save ${storageKey} to device storage`, e);
return false;
}
}
function loadLocalData(storageKey) {
try {
const savedData = localStorage.getItem(storageKey);
if (savedData) {
return JSON.parse(savedData);
}
} catch (e) {
console.error(`Failed to load ${storageKey} from device storage`, e);
}
return null;
}