Window/browser Session storage read in use API

sukumard5750513 07-06-2019

Hi,

I am trying to put some logic based on the value of a variable in session storage, based on session storage value i need to read some OSGI configuration. I am putting the read logic in use-API of HTL like below.

Component HTML file :-

<sly data-sly-use.clientLib="${'/libs/granite/sightly/templates/clientlib.html'}">

            <clientlib data-sly-call="${clientLib.js @ categories='digitalexp-ss-l9-navigation'}" data-sly-unwrap/>

        </sly>

            <div data-sly-use.roleLogic="clientlibs/navigation/js/role.js">

                Logged In Role :-  ${roleLogic.userRole}

</div>

JS file :-

"use strict";

use(function () {

var loggedInUserRole = window.sessionStorage.getItem("userRole");

    return {

        userRole:loggedInUserRole

    };

});

But i am getting below exception  even i dont include as clientLib:-

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: org.mozilla.javascript.EcmaError: ReferenceError: "window" is not defined.

could anyone give some pointer how to read session storage value which is being populated by some external REST services.

Note :- i am using @AEM 6.3.2.2

Accepted Solutions (1)

Accepted Solutions (1)

Arun_Patidar
MVP
07-06-2019

The window objects are just provided by web-browsers and are not part of the ECMAScript standard which Rhino implements. They are there to allow a script to access the current browser window and the HTML document. The document object is actually an implementation of the W3C DOM.

The local storage, as its name indicates, stores information locally, which means in the browser, at client-side. The JS server API executes at server-side. There is no way to access the local storage at server-side.

If you need to put some logic based on role, then retrieve this ID from the local storage in JavaSCript, and send a request containing this role to the server.

Answers (0)