Skip to main content
Dinu_Arya
Level 6
Dec 1, 2015 6:24 PM UTC
Solved

How to get current logged in user in JS

Hi Team,

I need to get current logged in user information using JavaScript. For this I need to get user's id. Please let me know how can I get the user id so that I can pass it to a servlet and get the user's info.

Thanks,

AryA.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
Level 10
Dec 2, 2015 7:28 AM UTC

@Dinu,

"cq.widgets" is the dependency which you should be using. However, "cq.widgets" loads many libraries, So, you better go use it carefully. You might have to find out useless libraries which come with "cq-widgets".

--

jitendra

7 replies

smacdonald2008
Level 10
Dec 1, 2015 8:18 PM UTC

See this older community thread: https://forums.adobe.com/thread/1246415

Translate
Kunal_Gaba_
New Member
Dec 2, 2015 12:15 AM UTC

You can get the current logged in user Id using the following code - 

CQ.User.getCurrentUser().getUserID();
Translate
Jitendra_S_Toma
Level 10
Dec 2, 2015 4:06 AM UTC

Just make sure "CQ" object does exist in a publish environment as well.

Translate
kautuk_sahni
Level 9
Dec 2, 2015 7:17 AM UTC

Hi

I have found stackoverflow answer to do the same:-

In JSP / Java you can adapt your resource to UserManager class and get the current user or list down all the users and groups as per your requirement.

    Session session = resourceResolver.adaptTo(Session.class); UserManager userManager = resourceResolver.adaptTo(UserManager.class); /* to get the current user */ Authorizable auth = userManager.getAuthorizable(session.getUserID()); /* to get the property of the authorizable. Use relative path */ Value[] names = auth.getProperty("./profile/familyName"); /* to get the groups it is member of */ Iterator<Group> groups = auth.memberOf();

To list all the users or groups, you can use findAuthorizables() method available in the UserManger.

You can also obtain the User info in JS using CQ.User.getCurrentUser() which would return an instance of the current User, with which you can access the user's properties.

I hope this would come as some help.

Thanks and Regards

Kautuk Sahni

Translate
Kautuk Sahni
Dinu_Arya
Dinu_AryaAuthor
Level 6
Dec 2, 2015 7:20 AM UTC

Hi Kunal,

It is not working in normal jQuery script. I'm getting "CQ is undefined" error. It works fine in listeners script. Please let me know the dependencies.

Thanks,

AryA.

Translate
Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
Level 10
Dec 2, 2015 7:28 AM UTC

@Dinu,

"cq.widgets" is the dependency which you should be using. However, "cq.widgets" loads many libraries, So, you better go use it carefully. You might have to find out useless libraries which come with "cq-widgets".

--

jitendra

Translate
Dinu_Arya
Dinu_AryaAuthor
Level 6
Dec 2, 2015 6:33 PM UTC

Hi Jitendra,

I'm able to get the user id now. Will it create any problem if I include this (cq.widgets) as a dependency to my client library?

Thanks,

AryA.

Translate