22

How I would go about getting the current users username or first and last name?

I have been looking at this for the last couple of hours - I was just wondering how I would go about getting their name and then displaying it on the page.

For example the current user is 'John Smith' and in the lightning component I want to output John Smith's name like:

Welcome to our community John Smith!

3
18

I wish there was concept like merge field in Lightning components where some functions were global and directly accessible but looks like only way to do this will be code with server side call.

Below is sample code

public with sharing class SimpleServerSideController {

//Use @AuraEnabled to enable client- and server-side access to the method
  @AuraEnabled
  public static String getUserName() {
    return userinfo.getName();
  }
 }

<aura:component controller="SimpleServerSideController">
   <aura:attribute name="Name" type="String"/>
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>

({
doInit: function(cmp){
    var action = cmp.get("c.getUserName");
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS") {
            cmp.set("v.Name", response.getReturnValue());
         }
      });
       $A.enqueueAction(action);
     }
 })
1
  • 1
    I agree, @MohithShrivastava. I $User value provider would be a welcome addition. – pchittum Dec 2 '15 at 10:30
6

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}

4
  • This does not really answer the question, Question is to get the User name, Not profile name – rahul gawale Nov 7 '19 at 10:29
  • 1
    Using this format you can get any of the user object fields. This is just a sample syntax provided. Use username in the Fields attribute to get the username. – Karthi G Nov 11 '19 at 13:44
  • Is this possible to do only in the JS file? let's say I don't want to show it in the UI but back in the JS I want to have the user's first and last name. Thanks! – Bahman.A Dec 2 '20 at 21:07
  • @Bahman.A This tag will not expose the value in UI. I think we can use the component.get expression to access the value in JS. – Karthi G Dec 4 '20 at 10:05
5

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"));
}
 })
2

You can add following method to your controller:

@AuraEnabled
public static String getUserFirstName() {
    return UserInfo.getFirstName();
}
0

You can vote for this idea to allow global user fields in lightning components reflecting current user information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy