Get Current User Info in LWC

by Rijwan Mohmmed
0 comment
get-current-user-info-in-lwc

Hello friends, today we will discuss Get Current User Info in LWC. We can get current User info like Id, Name, Email, IsActive, and Alias details without apex class.  In Salesforce Lightning Web Component (LWC), we use the UIRecordApi property to display the current User detail.

Also, check this : Lookup Field in LWC Datatable Inline Edit

get-current-user-info-in-lwc
get-current-user-info-in-lwc-output

Key Highlights :

  1. We don’t need apex class to get Current User info.
  2. We can fetch the Username, User Email, and User Id.
  3. We use UIRecordApi to get records.
  4. We can get the Current User Id just import.

Process & Code :

Get Current User Id :

import Id from '@salesforce/user/Id';

lWCCurrentUserInfo.Html :

<template>
    <!-----header start-------->
    <div class="slds-tabs_card">
		<div class="slds-page-header">
			<div class="slds-page-header__row">
				<div class="slds-page-header__col-title">
					<div class="slds-media">
						<div class="slds-media__figure">
							<span class="slds-icon_container slds-icon-standard-opportunity">
								 <lightning-icon icon-name="standard:recipe" alternative-text="recipe" title="recipe"></lightning-icon>
                            </span>
						</div>
						<div class="slds-media__body">
							<div class="slds-page-header__name">
								<div class="slds-page-header__name-title">
									<h1>
										<span>Get Current User Info in LWC</span>
										<span class="slds-page-header__title slds-truncate" title="Recently Viewed">TechDicer</span>
									</h1>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div> <br/>
    <!-----header end-------->
    <!-----User info Card --->
    <lightning-card  variant="Narrow"  title="Get Current User Info in LWC" icon-name="standard:user">
        <div class="slds-box slds-m-around_medium">
            <p><b>User Id :</b> {userId}</p>
            <p><b>User Name :</b> {currentUserName}</p>
            <p><b>User Email :</b> {currentUserEmail}</p>
            <p><b>User Is Active :</b> {currentIsActive}</p>
            <p><b>User Alias :</b> {currentUserAlias}</p>
        </div>
    </lightning-card>
    <!-----/User info Card -->
</template>

lWCCurrentUserInfo.JS:

import { LightningElement, wire, track } from 'lwc';
import Id from '@salesforce/user/Id';
import { getRecord } from 'lightning/uiRecordApi';
import UserNameFIELD from '@salesforce/schema/User.Name';
import userEmailFIELD from '@salesforce/schema/User.Email';
import userIsActiveFIELD from '@salesforce/schema/User.IsActive';
import userAliasFIELD from '@salesforce/schema/User.Alias';
 
 
export default class LWCCurrentUserInfo extends LightningElement {
    @track error;
    @track userId = Id;
    @track currentUserName;
    @track currentUserEmail;
    @track currentIsActive;
    @track currentUserAlias;

    @wire(getRecord, { recordId: Id, fields: [UserNameFIELD, userEmailFIELD, userIsActiveFIELD, userAliasFIELD ]}) 
    currentUserInfo({error, data}) {
        if (data) {
            this.currentUserName = data.fields.Name.value;
            this.currentUserEmail = data.fields.Email.value;
            this.currentIsActive = data.fields.IsActive.value;
            this.currentUserAlias = data.fields.Alias.value;
        } else if (error) {
            this.error = error ;
        }
    }
}

Output :

get-current-user-infos-in-lwc-Output

Reference :

  1. LWC get record Api

What’s your Reaction?
+1
6
+1
10
+1
2
+1
1
+1
2
+1
1

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.