Fix: save sessionid to localStorage as fallback for HttpOnly cookie

This commit is contained in:
Kato
2026-09-04 16:45:00 +00:00
parent 58dc9e4206
commit 6ca936671e
+15 -4
View File
@@ -8,23 +8,32 @@ document.addEventListener('DOMContentLoaded', () => {
function getCookie(name) {
console.log('DEBUG getCookie: all cookies =', document.cookie);
// Strategy 1: localStorage (for HttpOnly cookies)
const local = localStorage.getItem('sessionid');
if (local) {
console.log('DEBUG getCookie:', name, '=', local, '(from localStorage)');
return local;
}
// Strategy 2: document.cookie (non-HttpOnly cookies)
const raw = document.cookie;
// Strategy 1: semicolon prefix (cookies after first)
// Strategy 2a: semicolon prefix (cookies after first)
const parts1 = raw.split(`; ${name}=`);
if (parts1.length === 2) {
const val = parts1.pop().split(';').shift();
const cleaned = val.replace(/^"|"$/g, '');
console.log('DEBUG getCookie:', name, '=', cleaned);
console.log('DEBUG getCookie:', name, '=', cleaned, '(from cookie)');
return cleaned;
}
// Strategy 2: no semicolon prefix (FIRST cookie in document.cookie)
// Strategy 2b: no semicolon prefix (FIRST cookie in document.cookie)
const parts2 = raw.split(`${name}=`);
if (parts2.length === 2) {
const val = parts2.pop().split(';').shift();
const cleaned = val.replace(/^"|"$/g, '');
console.log('DEBUG getCookie:', name, '=', cleaned, '(first cookie)');
console.log('DEBUG getCookie:', name, '=', cleaned, '(from first cookie)');
return cleaned;
}
@@ -36,6 +45,8 @@ function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = name + '=' + encodeURIComponent(value) + ';expires=' + expires.toUTCString() + ';path=/';
// Also save to localStorage for HttpOnly cookies
localStorage.setItem(name, value);
}
const API_URL = 'https://n8n.odoo4projects.com/webhook/paraguay/login';