function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Balajee SelvarajBalajee Selvaraj 

How to use get set cookies in lightning components ?

ApexPages.currentPage(). setCookies(new Cookie[] { userCookie ...}) is not working in lightning component any workaround for this ?
Raj VakatiRaj Vakati
use javascript cookie API. Or you can use a platform cache. 
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
        user = prompt("Please enter your name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 365);
        }
    }
}


 
Raj VakatiRaj Vakati
You can use localStorage as another option.  LockerService throws error when saving a document cookie

https://salesforce.stackexchange.com/questions/128456/lockerservice-throws-error-when-saving-a-document-cookie

https://salesforce.stackexchange.com/questions/154415/locker-service-document-cookie-support
Fenil Mehta 17Fenil Mehta 17
Hello Raj,
I have to generate cookie login user name with the login username, password custom field values in lighnting component......
using platform cache in lightning component with apex class.....
can u help me out pls.....
Thanks in advance.....