简体   繁体   中英

Cookie set with javascript disappears after refresh

I'm setting my cookies like this:

document.cookie = `${cookieName}=${JSON.stringify(cookieObject)};path=/;SameSite=Strict;Secure=true;expires=${someDate.toUTCString()};`

Everything is fine at first. But when I refresh, the cookie disappears and firefox console tells me a message:

Cookie “thecookiename” will be soon rejected because it has the “SameSite” attribute set to “None” or an invalid value, without the “secure” attribute.

If I refresh again, the message disappears and the cookie is also gone.

Chrome acts the same, but minus the message.

Edit

I realized, the problem might be related to the fact, that I'm overriding the document.cookie setter.

document.setCookie = document.__lookupSetter__('cookie');
document.newCookieSetter= (cookie) => {
    // here I check, whether a cookie is allowed or not. Calls for an api endpoint to get the information.

    if (cookieDataRecieved() && cookieIsAllowed(cookie)) {
        return document.setCookie(cookie);
    }
    cacheCookie(cookie); // If cookie data has loaded, try checking again.  
}

document.__defineSetter__('cookie', document.newCookieSetter);

The problem applies to every cookie added through the document.cookie = "somecookie";

Secure属性没有值,它只是一个裸属性(存在或不存在),因此只需将Secure=true替换为Secure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM