19

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!

  • 1
    See sfdcmonkey.com/2018/01/08/display-current-user-information for a more detailed server side solution (provides more information about the user) – Bryan Jun 13 '18 at 2:17
  • Also from that same post is a direct way to get the User ID. Not what you wanted but might be useful to someone else coming to this question (like I did) var userId = $A.get("$SObjectType.CurrentUser.Id"); – Bryan Jun 13 '18 at 2:41
15

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);
     }
 })
| improve this answer | |
  • 1
    I agree, @MohithShrivastava. I $User value provider would be a welcome addition. – pchittum Dec 2 '15 at 10:30
4

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

You can add following method to your controller:

@AuraEnabled
public static String getUserFirstName() {
    return UserInfo.getFirstName();
}
| improve this answer | |
2

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}

| improve this answer | |
  • 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
0

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

| improve this answer | |

Your Answer

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