I'm trying to save Tailwind's darkmode cookies, but Laravel then won't read the cookie to me.
I used JavaScript to save the cookies:
function setCookie(name, value) {
var d = new Date();
d.setTime(d.getTime() + (365*24*60*60*1000));
var expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
themeToggleBtn.addEventListener('click', function() {
// if set via local storage previously
if (localStorage.getItem('color-theme')) {
if (localStorage.getItem('color-theme') === 'light') {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
setCookie('theme', 'dark');
} else {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
setCookie('theme', 'light');
}
// if NOT set via local storage previously
} else {
if (document.documentElement.classList.contains('dark')) {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
setCookie('theme', 'light');
} else {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
setCookie('theme', 'dark');
}
}
});
However, when I use:
$theme = \Cookie::get('theme');
Laravel does not interpret the content correctly. I checked the Cookie and I have the plain text "Dark", and not encrypted as Laravel saves them using Cookie::queue.
How do I get Laravel to read it?