I am making Cookie Consent for website and the only problem I have right now is that Cookie is stored in that session only, and I also want to set path to my root domain.
Currently my JS is like this:
const cookieStorage = {
getItem: (item) => {
const cookies = document.cookie
.split(';')
.map(cookie => cookie.split('='))
.reduce((acc, [key, value]) => ({ ...acc, [key.trim()]: value }), {});
return cookies[item];
},
setItem: (item, value) => {
document.cookie = `${item}=${value};`
}
}
const storageType = cookieStorage;
const consentPropertyName = 'Cookie By: RohanPhuyal ';
const shouldShowPopup = () => !storageType.getItem(consentPropertyName);
const saveToStorage = () => storageType.setItem(consentPropertyName, true);
window.onload = () => {
const acceptFn = event => {
saveToStorage(storageType);
consentPopup.classList.add('hidden');
}
const consentPopup = document.getElementById('consent-popup');
const acceptBtn = document.getElementById('accept');
acceptBtn.addEventListener('click', acceptFn);
if (shouldShowPopup(storageType)) {
setTimeout(() => {
consentPopup.classList.remove('hidden');
}, 1000);
}
};
How can I add expiry time (age) on cookie and also set path=/ ?