Problem
In Aura components you often need to read information about the currently logged-in user (for personalization or access control). The goal is to get the current user name and the current user profile name without calling an Apex controller.
Solution Overview
You can use the client-side record API (force:recordData / lightning:recordForm / lightning:recordViewForm) in Aura to load the User record for the current user by using the global token for the current user’s Id: {!$SObjectType.CurrentUser.Id}. Once the User record is loaded into a component attribute, you can read the Name and Profile relationship fields directly in JavaScript.
Why this works
force:recordData leverages the Lightning Data Service (LDS) on the client — no Apex required. LDS can load a record by Id and provide its fields (including relationship fields like Profile.Name) to your component. This is safe, fast, and respects sharing and CRUD rules.
Example Aura component (markup)
<aura:component implements="flexipage:availableForAllPageTypes" access="global">
<aura:attribute name="currentUser" type="User" />
<!-- Load the current user record via Lightning Data Service -->
<force:recordData aura:id="userLoader"
recordId="{!$SObjectType.CurrentUser.Id}"
targetFields="{!v.currentUser}"
fields="Id, Name, Profile.Name"
mode="VIEW" />
<div>
<strong>User Name:</strong> {!v.currentUser.Name}<br/>
<strong>Profile Name:</strong> {!v.currentUser.Profile.Name}
</div>
</aura:component>
JavaScript (controller/helper) access
If you need to use these values in your component controller/helper (for conditional logic or API calls) you can read the currentUser attribute after LDS has populated it. Example in controller:
({
doInit : function(component, event, helper) {
// If needed, you can access the attribute directly
var user = component.get("v.currentUser");
if(user && user.Id) {
var userName = user.Name; // current user name
var profileName = user.Profile ? user.Profile.Name : null; // profile name
console.log('User:', userName, 'Profile:', profileName);
}
}
})
Important notes & troubleshooting
- Be sure to include the relationship field in the
fieldslist (e.g.,Profile.Name) if you need the profile name. - Sometimes UI caching means
v.currentUserisn’t immediately available on init. If you see undefined values, use a small timeout or react to therecordUpdatedevent fromforce:recordDatato detect when data is ready. - LDS respects sharing and field-level security. If a field is not accessible to the running user, it won’t be returned.
- If you only need the user Id in markup,
{!$SObjectType.CurrentUser.Id}is available directly in expressions.
Alternative
In LWC you’d use the getRecord wire adapter from lightning/uiRecordApi. For Aura, force:recordData is the recommended no-Apex approach.
This approach keeps everything client-side, avoids Apex calls, and leverages Lightning Data Service for performance and security.








Leave a Reply