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
Kevin Jackson 11Kevin Jackson 11 

how to reference userid in lightning component

Hi, I am trying to a field from the active user in a component and use it in an if statement.

 <aura:if isTrue="{!$User.IsActive}">
    <td class="button"><a href="#" target="_blank">
               <lightning:button label="Join"/></a></td>
    <aura:set attribute="else">
        <td class="button"><a href="#" target="_blank">
               <lightning:button label="Renew Membership"/></a></td>
    </aura:set>
</aura:if>


I assume I am using the wrong syntax for {!$User.IsActive} as it always throws up the else and IsActive is definitely TRUE for the current user.

Thanks for any help
Dorian Sutton 9Dorian Sutton 9
Hi Kevin,

Wouldn't the user always be active if they can open your component?

Anyway, the $User global is not available in Lightning Components.

If you need to retrieve details about the User you will need to add an Apex controller with an @AuraEnabled method which returns those details to your component, something like:
public class myController {

    @AuraEnabled
    public static Boolean isActiveUser() {
        Id userId = UserInfo.getUserId();
        return [SELECT IsActive FROM User WHERE Id = :userId].IsActive;
    }

}
You can then call this method from your JavaScript controller, with a callback method to update an attribute in your component that you can use in your aura:if tag, see here:
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm
Kevin Jackson 11Kevin Jackson 11
Indeed!  user should always be active :-)    I was testing a variable I was certain would be true in all cases. I actually need a different variable, but was just testing with this one.

Thanks for the info (and the code) I will give it a try.
Kevin Jackson 11Kevin Jackson 11
Dorian,

I need a little extra help.  I added the code to my controller, but I am not sure how to set the variable to the current state in my component.  Would you be able to post a small snippet on how I would call it?

Thanks.
Dorian Sutton 9Dorian Sutton 9
Ok, so in the component you're going to need an aura:attribute for the active result, and (if you don't have one already) an "init" handler to call the init method in your JavaScript controller:
<aura:attribute name="isActiveUser" type="Boolean"/> 
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

Then, in your JavaScript controller, you'll need a "doInit" method that fires your AuraEnabled Apex method:
 
({ 
  doInit : function(component, event, helper) { 
    var myAction = component.get("c.isActiveUser"); 
    
    myAction.setCallback(this, function(response) { 
      if(response.getState() === "SUCCESS") { 
        component.set("v.isActiveUser", response.getReturnValue()); 
    }}); 

    $A.enqueueAction(myAction); 
  } 
})
brahmaji tammanabrahmaji tammana
Vote for this idea - https://success.salesforce.com/ideaView?id=0873A000000E7lFQAS to allow global user fields reflecting current user information.
Pramodh KumarPramodh Kumar
You No longer require writing the apex class to get the userId

Here the controller code for the lightning
 
var userId = $A.get("$SObjectType.CurrentUser.Id");
Console.log(userId);

simply use this statement. Same thing I have posted on my blog, please check out my post (http://allaboutlightning.com/get-current-user-id-information-in-lightning/)

Thanks
Pramodh
allaboutlightning.com
Javier CGJavier CG
Pramodh, I like your solution for the controller code in lightning.
Do you know if exists another way like this to get the profile info without asking the server?
I am searching and don´t find anything