userInfo

How to get Logged-In User Info in LWC Without server call

LWC

Due to LWC component development mostly time we needed current user information like Id, Name, Email, IsActive, ProfileName, Role and Manager etc. Previously when we developed AURA component then we need to write APEX method to get logged-in user Info, but for better performance LWC provide UIRecordApi method that’s will help to get logged-In user details without Apex Method.

UIRecordApi property to display the current User detail without apex method, So We can get current User info like Id, Name, Email, IsActive.

This is LWC features that’s will manage by client side without hit server.

Benefit:-

  1. By The help of import method we can get current user Id (import Id from ‘@salesforce/user/Id’)
  2. Import UIRecordApi to get records.
  3. We don’t need to write apex method to get Current User info

UserInfo.html

<template>  
    <lightning-card title="Logged in User Details" icon-name="standard:user" style="background: gray">
        <div class="slds-m-around_medium slds-list_horizontal slds-wrap">
            <table class="slds-table slds-table_bordered slds-table_fixed-layout" border="1px solid white;" bordercolor="#96D4D4" role="grid">
                <tr>
                    <th style="background-color: #96D4D4; border: 1px solid black"><b>Id</b></th>
                    <td>{userId}</td>
                </tr>
                <tr>
                    <th style="background-color: #96D4D4; border: 1px solid black"><b>Name</b></th>
                    <td>{loggedInUserName}</td>
                </tr>
                <tr>
                    <th style="background-color: #96D4D4; border: 1px solid black"><b>Email</b></th>
                    <td>{loggedInUserEmail}</td>
                </tr>
                <tr>
                    <th style="background-color: #96D4D4; border: 1px solid black"><b>IsActive</b></th>
                    <td>{loggedInUserIsActive}</td>
                </tr>
                <tr>
                    <th style="background-color: #96D4D4;border: 1px solid black"><b>Alias</b></th>
                    <td>{loggedInUserAlias}</td>
                </tr>
                <tr>
                    <th style="background-color: #96D4D4; border: 1px solid black"><b>Profile Name</b></th>
                    <td>{loggedInUserProfileName}</td>
                </tr>
                <tr>
                    <th style="background-color: #96D4D4; border: 1px solid black"><b>Role Name</b></th>
                    <td>{loggedInUserRoleName}</td>
                </tr>
                <tr>
                    <th style="background-color: #96D4D4; border: 1px solid black"><b>Manager Name</b></th>
                    <td>{loggedInUserManagerName}</td>
                </tr>
            </table>
        </div>
    </lightning-card>

</template>

UserInfo.js

import { LightningElement,wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

import Id from '@salesforce/user/Id';
import NameFIELD from '@salesforce/schema/User.Name';
import EmailFIELD from '@salesforce/schema/User.Email';
import IsActiveFIELD from '@salesforce/schema/User.IsActive';
import AliasFIELD from '@salesforce/schema/User.Alias';
import RoleFiled from '@salesforce/schema/User.UserRole.Name';
import ProfileField from '@salesforce/schema/User.Profile.Name'; 
import ManagerField from '@salesforce/schema/User.Manager.Name'; 


export default class UserInfo extends LightningElement {
    userId = Id;
    loggedInUserName;
    loggedInUserEmail;
    loggedInUserIsActive;
    loggedInUserAlias;
    loggedInUserRoleName;
    loggedInUserProfileName;
    loggedInUserManagerName;
    error;

    @wire(getRecord, { recordId: Id, fields: [NameFIELD, EmailFIELD, IsActiveFIELD, AliasFIELD ,RoleFiled,ProfileField,ManagerField]}) 
    loggedInUserInfo({error, data}) {
        if (data) {

            console.log('data===>>>>> '+JSON.stringify(data.fields.Profile.value.fields.Name.value));
            this.loggedInUserName = data.fields.Name.value != null ? data.fields.Name.value : '';
            this.loggedInUserEmail = data.fields.Email.value != null ? data.fields.Email.value : '';
            this.loggedInUserIsActive = data.fields.IsActive.value != null ? data.fields.IsActive.value : '';
            this.loggedInUserAlias = data.fields.Alias.value != null ? data.fields.Alias.value : '';
            this.loggedInUserProfileName = data.fields.Profile.value.fields.Name.value != null ? data.fields.Profile.value.fields.Name.value : '';
            this.loggedInUserRoleName = data.fields.UserRole.value.fields.Name.value != null ? data.fields.UserRole.value.fields.Name.value : '';
            this.loggedInUserManagerName = data.fields.Manager.value.fields.Name.value != null ? data.fields.Manager.value.fields.Name.value : '';

        } else if (error) {
            this.error = error ;
            console.log('error==>>> '+JSON.stringify(error));
        }
    }


}

uniquesymbol

Leave a Reply