forked from codinit-dev/codinit-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.ts
More file actions
40 lines (35 loc) · 1.1 KB
/
Copy pathcookie.ts
File metadata and controls
40 lines (35 loc) · 1.1 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
import { session } from 'electron';
import { DEFAULT_PORT } from './constants';
import { store } from './store';
/**
* On app startup: read any existing cookies from store and set it as a cookie.
*/
export async function initCookies() {
await loadStoredCookies();
}
// Function to store all cookies
export async function storeCookies(cookies: Electron.Cookie[]) {
for (const cookie of cookies) {
store.set(`cookie:${cookie.name}`, cookie);
}
}
// Function to load stored cookies
async function loadStoredCookies() {
// Get all keys that start with 'cookie:'
const cookieKeys = store.store ? Object.keys(store.store).filter((key) => key.startsWith('cookie:')) : [];
for (const key of cookieKeys) {
const cookie = store.get(key);
if (cookie) {
try {
// Add default URL if not present
const cookieWithUrl = {
...cookie,
url: cookie.url || `http://localhost:${DEFAULT_PORT}`,
};
await session.defaultSession.cookies.set(cookieWithUrl);
} catch (error) {
console.error(`Failed to set cookie ${key}:`, error);
}
}
}
}