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

5 Answers 5

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
    Commented Dec 2, 2015 at 10:30
9

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 Commented Nov 7, 2019 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. Commented Nov 11, 2019 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
    Commented Dec 2, 2020 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. Commented Dec 4, 2020 at 10:05
5

We can fetch from email field if it is '[email protected] 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 Allow global User Merge fields in Lightning Components reflecting current user information.

You must log in to answer this question.