Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Retrieve logged in user email

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

@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

Avatar

Community Advisor

From AuthenticationInfo you will get User ID only.

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

 

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
}

//===
}