Retrieve logged in user email | Community
Skip to main content
robertol6836527
Level 4
February 27, 2024
Solved

Retrieve logged in user email

  • February 27, 2024
  • 3 replies
  • 1173 views

HI,

on Adobe Aem Author in a java class that implements AuthenticationHandler, AuthenticationFeedbackHandler I have the following method:

 

public boolean authenticationSucceeded(HttpServletRequest request, HttpServletResponse response, AuthenticationInfo authInfo) {

}

 

How can I obtain the email address of the logged in user within the authenticationSucceeded method ?

 

Thank you

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shashi_Mulugu

@robertol6836527 within authenticationSucceeded method, you can use authInfo object to get username like authInfo.getUser() and then use AEM UserManager API to get further details about user.

https://www.albinsblog.com/2015/03/how-to-get-userinfo-through-java-api-in.html

3 replies

Imran__Khan
Community Advisor
Community Advisor
February 27, 2024

You can use below code snippet to get user details available in session

import javax.jcr.Session; import org.apache.jackrabbit.api.security.user.Authorizable; import org.apache.jackrabbit.api.security.user.UserManager; ... UserManager userManager = resourceResolver.adaptTo(UserManager.class); Session session = resourceResolver.adaptTo(Session.class); log.info("User="+session.getUserID()); or.. Authorizable auth = userManager.getAuthorizable(session.getUserID()); log.info("\n--- User, Principal="+auth.getID()+","+auth.getPrincipal().getName());

 https://stackoverflow.com/questions/33132102/aem-get-current-userid

Shashi_Mulugu
Community Advisor
Shashi_MuluguCommunity AdvisorAccepted solution
Community Advisor
February 27, 2024

@robertol6836527 within authenticationSucceeded method, you can use authInfo object to get username like authInfo.getUser() and then use AEM UserManager API to get further details about user.

https://www.albinsblog.com/2015/03/how-to-get-userinfo-through-java-api-in.html

SureshDhulipudi
Community Advisor
Community Advisor
February 27, 2024

From AuthenticationInfo you will get User ID only.

https://developer.adobe.com/experience-manager/reference-materials/6-4/javadoc/org/apache/sling/auth/core/spi/AuthenticationInfo.html

 

public boolean authenticationSucceeded(HttpServletRequest request, HttpServletResponse response, AuthenticationInfo authInfo) {

String userId = authInfo.getUser();

//======

 

ResourceResolver resolver// get resource resolver 
Session session = resolver.adaptTo(Session.class); // get session 
UserManager userManager = AccessControlUtil.getUserManager(session);
Authorizable authorizable = userManager.getAuthorizable(userId);
if (authorizable != null && authorizable instanceof User) {
User user = (User) authorizable;
Profile profile = user.getProfile();
String email = profile.getEmail();
// Now you have the email
}

//===
}