Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Detect if user is logged in with sightly

Avatar

Level 2

Hello,

I wanted to detect if an user is logged in or not using sightly. I know because of this link: https://docs.adobe.com/docs/en/aem/6-0/develop/sightly/global-objects.html

That I have access to the currentSession from sightly. Sadly I cannot find any documentation on how to use this object, I was reading the java documentation of javax.servlet.http.HttpSession but I have no idea how to use it within sightly. Does somebody have examples regarding this currentSession object or even better knows how to detect if the user is logged in or not using sightly only.

 

Thanks for reading and all the help you can provide

1 Accepted Solution

Avatar

Correct answer by
Level 4

${currentSession.userID} 

The above sightly expression should give you the details of the current logged in user. If this returns a value of null / anonymous (not sure which value is returned) then there is no logged in session available.

View solution in original post

6 Replies

Avatar

Level 10

You cannot log in to AEM using Sightly. Sightly is a template language that you use to create components that an author drops into an AEM web page (as opposed to JSP).  We have a lot of community sourced articles that discuss how to build AEM components using Sightly. See this thread for a reference of Sightly articles. 

http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

Plus - we are having a webinar on Sightly later this month for all community members: 

http://scottsdigitalcommunity.blogspot.ca/2016/03/ask-aem-community-experts-deep-dive.html

BTW - you do not use  javax.servlet.http.HttpSession in AEM. You work with JCR Sessions. 

https://www.day.com/maven/jsr170/javadocs/jcr-2.0/javax/jcr/Session.html

Hope this helps. 

Avatar

Level 2

I did not mean to Log in using sightly. According to the documentation I have access to some global objects from sightly, One of them is called currentSession, and for a description it says "javax.servlet.http.HttpSession" in the adobe documentation see docs

I just wanted to know if by using this I could detect if there is a valid session or not (Someone is logged in or not)

Avatar

Level 10

OK - that makes more sense. You can access all objects easily using Sightly, For example - notice on that list - there is wcmmode. You can get this object in Sightly using this syntax: 

<div data-sly-test="${wcmmode.edit}">Show this only in edit mode to the author</div>

Likewise, consider currentPage.  You can get this object in Sightly using this syntax: 

<div>currentPageName : ${currentPage.name}</div>

You should be able to get all objects on that list using:

${ObjectName}

See all five blog articles here:

http://blogs.adobe.com/experiencedelivers/experience-management/sightly-intro-part-1/

As well to learn more about Sightly - please sign up to March version of Ask the COmmuiity Experts - this month Praveen Is presenting - a top AEM COmmuntiy member and very knowledge on Sightly. 

Avatar

Correct answer by
Level 4

${currentSession.userID} 

The above sightly expression should give you the details of the current logged in user. If this returns a value of null / anonymous (not sure which value is returned) then there is no logged in session available.

Avatar

Administrator

Hi

As mentioned by Naveen, you can use ${currentSession.userID}. apart from this, please find below the list of objects that could be used:

Java-backed Objects

These objects provide the standard AEM execution context (as global.jsp does for JSPs, for example). Each object is backed by the corresponding Java object.

                                                                                             
Variable NameDescription
componentcom.day.cq.wcm.api.components.Component
componentContextcom.day.cq.wcm.api.components.ComponentContext
currentDesigncom.day.cq.wcm.api.designer.Design
currentNodejavax.jcr.Node
currentPagecom.day.cq.wcm.api.Page
currentSessionjavax.servlet.http.HttpSession
currentStylecom.day.cq.wcm.api.designer.Style
designercom.day.cq.wcm.api.designer.Designer
editContextcom.day.cq.wcm.api.components.EditContext
logorg.slf4j.Logger
outjava.io.PrintWriter
pageManagercom.day.cq.wcm.api.PageManager
readerjava.io.BufferedReader
requestorg.apache.sling.api.SlingHttpServletRequest
resourceorg.apache.sling.api.resource.Resource
resourceDesigncom.day.cq.wcm.api.designer.Design
resourcePagecom.day.cq.wcm.api.Page
responseorg.apache.sling.api.SlingHttpServletResponse
slingorg.apache.sling.api.scripting.SlingScriptHelper
slyWcmHelpercom.adobe.cq.sightly.WCMScriptHelper
wcmmodecom.day.cq.wcm.api.WCMMode (in Sightly, evaluates to a string, either "EDIT" or "DESIGN")
xssAPIcom.adobe.granite.xss.impl.XSSAPImpl

Link:- https://docs.adobe.com/docs/en/aem/6-1/develop/sightly/global-objects.html

I hope this will help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 2

Thanks Naveem, that was exactly what I needed a thousand brownie points for you.