function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gaston LeferGaston Lefer 

How to display the current user name in lighting?

Just trying to display the current user info on a lightning page
Best Answer chosen by Gaston Lefer
sfdcMonkey.comsfdcMonkey.com
for this you have need to on component load call the apex method and fetch the current login user information, here is the sample

apex class controller :
public class currentUserInfoCtrl {
   @AuraEnabled 
    public static user fetchUser(){
       User u = [select id,Name from User where id =: userInfo.getUserId()];
        return u;
    }
}
lightning component :
<aura:component controller="currentUserInfoCtrl">
  <aura:handler name="init" value="this" action="{!c.doInit}"/>
  <aura:attribute name="userInfo" type="user"/>  
    Current User Name : {!v.userInfo.Name}
</aura:component>
javaScript controller :
({
	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();
                component.set("v.userInfo", storeResponse);
            }
        });
        $A.enqueueAction(action);
	}
})

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks
 http://sfdcmonkey.com

 

All Answers

sfdcMonkey.comsfdcMonkey.com
for this you have need to on component load call the apex method and fetch the current login user information, here is the sample

apex class controller :
public class currentUserInfoCtrl {
   @AuraEnabled 
    public static user fetchUser(){
       User u = [select id,Name from User where id =: userInfo.getUserId()];
        return u;
    }
}
lightning component :
<aura:component controller="currentUserInfoCtrl">
  <aura:handler name="init" value="this" action="{!c.doInit}"/>
  <aura:attribute name="userInfo" type="user"/>  
    Current User Name : {!v.userInfo.Name}
</aura:component>
javaScript controller :
({
	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();
                component.set("v.userInfo", storeResponse);
            }
        });
        $A.enqueueAction(action);
	}
})

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks
 http://sfdcmonkey.com

 
This was selected as the best answer
Gaston LeferGaston Lefer
The code already has a doinit function where/how do I add this js controller?
sfdcMonkey.comsfdcMonkey.com
if you have already doinit function in your js controller then add below code in your doinit function at top or bottom anywhere:
and follow above code :
var action = component.get("c.fetchUser");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
                component.set("v.userInfo", storeResponse);
            }
        });
        $A.enqueueAction(action);

Thanks let me know if it helps you
sfdcMonkey.comsfdcMonkey.com
if you have any issue with this you can ask here, or if you got your solution kindly close this query by choosing best answer 
thanks 
Gaston LeferGaston Lefer
Doesn’t work :/
what if there’s an existing @auraenabled class
and aura:handler name="init" value="this" action="{!c.doInit}"/> already day exists? Thanks again 
sfdcMonkey.comsfdcMonkey.com
can you share your existing componnet and apex all code
 thanks 
Gaston LeferGaston Lefer
Do you know how to use?

var userId = $A.get("$SObjectType.CurrentUser.Id");
Console.log(userId);

from http://allaboutlightning.com/get-current-user-id-information-in-lightning/
 
sfdcMonkey.comsfdcMonkey.com
checkout post on it :
http://sfdcmonkey.com/2018/01/08/display-current-user-information/

and with below code :
var userId = $A.get("$SObjectType.CurrentUser.Id");
Console.log(userId);
you can only get the current user ID, in your client side javaScript controller

 
Gaston LeferGaston Lefer
ok, how can I modify the code above to make it works, i added everything you but doesnt work, just me where to add it.

I am actually trying to get the contact id at 146 but just even getting the name first wont work
Gaston LeferGaston Lefer
I was able to make it work, <force:inputField value="{!v.userInfo.Name}" /> how do i show contact id and account id
SamHowleSamHowle
@Piyush thank you!
Shama Mansuri 1Shama Mansuri 1
We can use below peice of code to avoid unnecessary calls between the server and client in order to fetch user details.
var userId = $A.get("$SObjectType.CurrentUser.Id");
Console.log(userId);
Vemulapalli SurendraVemulapalli Surendra
We can fetch from email field if it is  'FirstName.LastName@company.com 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.get("v.UserName"));
}
 })
Karthi GKarthi G
0
We can make use of Lightning Data service to access any of the user fields on the Lightning component without need to use apex controller.
Example:
<aura:attribute name="currentUser" type="User"/> <force:recordData aura:id="recordLoader" recordId="{!$SObjectType.CurrentUser.Id}" fields="Profile.Name" targetFields="{!v.currentUser}"/>

Access the value using the syntax: {!v.currentUser.Profile.Name}
Saurabh Verma 83Saurabh Verma 83
@Karthi , worked great !