
How to Display Current User Information In Lightning Component – Sample
Sometimes we have need to display information of current login user on lightning component or need to use in client side JavaScript controller.
Today in this post we are going to learn about how we can display current user information on salesforce custom lightning component. and also learn how to get current user ID without calling server side apex controller.
Step 1 : Create a Apex Controller : currentUserInfoCtrl.apxc
public with sharing class currentUserInfoCtrl { @AuraEnabled public static user fetchUser(){ // query current user information User oUser = [select id,Name,TimeZoneSidKey,Username,Alias,Country,Email,FirstName,LastName,IsActive,IsPortalEnabled FROM User Where id =: userInfo.getUserId()]; return oUser; } }
Step 2 : Create Lightning Component : userInfo.cmp
<aura:component controller="currentUserInfoCtrl"> <!-- init handler event call "doInit" function on component load and fetch current user information --> <aura:handler name="init" value="this" action="{!c.doInit}"/> <aura:attribute name="userInfo" type="user"/> <b>Current User Information</b> <p>Name : {!v.userInfo.Name}</p> <p>FirstName : {!v.userInfo.FirstName}</p> <p>FirstName : {!v.userInfo.FirstName}</p> <p>LastName : {!v.userInfo.LastName}</p> <p>Email : {!v.userInfo.Email}</p> <p>Username : {!v.userInfo.Username}</p> <p>IsActive : {!v.userInfo.IsActive}</p> <p>IsPortalEnabled : {!v.userInfo.IsPortalEnabled}</p> <p>TimeZoneSidKey : {!v.userInfo.TimeZoneSidKey}</p> <p>Country : {!v.userInfo.Country}</p> </aura:component>
userInfoController.js
({ doInit : function(component, event, helper) { var action = component.get("c.fetchUser"); action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { var storeResponse = response.getReturnValue(); // set current user information on userInfo attribute component.set("v.userInfo", storeResponse); } }); $A.enqueueAction(action); } })
Step 3 : Create Lightning Application for Testing: demo.app
From Developer Console >> File >> New >> Lightning Application
<aura:application extends="force:slds"> <c:userInfo/> <!-- here c: is org. default namespace prefix--> </aura:application>
Output :
If you want only current user ID, than you don’t need use apex controller. you can get current login user id in client side JavaScript controller using below statement :
({ doInit : function(component, event, helper) { var userId = $A.get("$SObjectType.CurrentUser.Id"); alert(userId); } })check also : How to access apex class property in lightning component
Please like our facebook page for new post updates. & Don’t forget to bookmark this site for your future reference.
if you have any suggestions or issue with it, you can share your thoughts in comment box.
Happy Learning 🙂
7 comments
thanks good post piyush soni,
How to get all list of logged users in Salesforceforce org and Display in Lightning component
Hi,
Can you please provide help on to get specific users timezone
Thanks
hello anwesh_sfdc have you find a solution ?
I have the same probleme
How to get user login user phone number in apex code?
Hi could you provide test class for this please?
If email address is ‘[email protected] on user profile, we can fetch the User name from email on user profile without server call.
Lightning Component :
<aura:component implements=”flexipage:availableForRecordHome>
<aura:attribute name=”UserName” type=”String” default=””/>
</aura:component>
javaScript controller :
({ doInit : function(component, event, helper) {
var UserName = $A.get(“$SObjectType.CurrentUser.Email”);
cmp.set(“v.UserName”,(UserName.substring(0, UserName.lastIndexOf(“@”))));
alert(cmp.set(“v.UserName”));
}
})
Hello, I am using a very similar code to display the ” Welcome ‘user name’ !” on a community page. it displays the name for Admin Users , but not for everyone. what am I missing? Thank you