2

All,

Is it possible to read cookies in an LWC component? (I think it is possible through Visualforce pages, but I need to do this in an LWC component)

According to the documentation, I should get all the cookies when i fire this command:

Cookie ck= ApexPages.currentPage().getCookies().get('cookie_name');

But, I always get a null value when I do

ApexPages.currentPage()

Edited: This component will be present on the main community login page. Here is the class:

public with sharing class CookieSync {
public CookieSync() {

}
@AuraEnabled
public static String customSync(){
    System.debug('checkpoint1');
    system.debug(ApexPages.currentPage()); //returns null
    // logic omitted.
    return 'test';
}

}

1
  • Hi, welcome to SFSE. Please take a tour and read How to Ask. Can you give us more context? Where the LWC component is embedded, how does its code look like, on which event you launch Apex, how does Apex code look like? Commented Mar 18, 2020 at 12:24

1 Answer 1

1

The @AuraEnabled method is not called in an ApexPages context as one would expect. Much of the ApexPages documentation only applies to VisualForce and Aura components.

On the client side, the LWC component is limited in accessing document.cookie - only cookies that were also set from the LWC component can be accessed. This is due to the "Secure" Locker API: https://developer.salesforce.com/docs/component-library/tools/locker-service-viewer.

So it is kind of possible to read cookies in an LWC component, but you need to "manually" pass it as a parameter on the @AuraEnabled method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .