Get Current User Details in Lightning Web Components

Let’s discuss here how to get the current user details in lightning web components. To access the current user Id, import @salesforce/user/Id in a component’s JavaScript class. Note that @salesforce/user/ cannot be imported by itself; you must indicate the property that you want to import. For the User standard object, only the Id field is available.

 

Option 1: Using Wire Service

You can able to get the current user details using getRecord wire adapter and you can able to pass the user id from the imported modules. Create a new lightning web component using the below SFDX command

Use the below userinfoexample.html code

Use the below userinfoexample.js code

 

Use the below userinfoexample.js-meta.xml

 

Push changes and you can able to see the user details

Option 2: – Using apex class

you can able to get the user detail even using the apex class .create an Apex class that returns the user details as shown below

 

update the userinfoexample.html with below code

 

 

update userinfoexample.js code as shown below

Push changes and you can able to see the current logged in user details