Friday, 11 January 2019

How to get current user id in lightning web component | Access logged in user ID in LWC


 To get current user id in LWC we need to import the "@salesforce/user/Id" Salesforce module.

 Syntax :-
import userId from '@salesforce/user/Id';

Sample Code :-

 showUserId.html
<template>
    <lightning-card title="Show User Detail" icon-name="custom:custom15">
        <div class="slds-m-around_medium">
            <p>User Id: {userId} </p>
        </div>
    </lightning-card>
</template>

 showUserId.js
import { LightningElement } from 'lwc';
import strUserId from '@salesforce/user/Id';

export default class ShowUserDetail extends LightningElement {
    userId = strUserId;
}

 showUserId.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="ShowUserDetail">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>

    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>

</LightningComponentBundle>

Output :-



Please check our old post on Lightning Web Components:-
Feel free to post your feedback or question.

Thanks,
Amit Chaudhary

6 comments:

  1. Thanks for sharing this post.
    Really helpful to me.

    ReplyDelete
  2. Hello Amit,

    I want to implement a Login page Community with the help of LWC. Please guide.

    Thanks,
    Deepak

    ReplyDelete
  3. is there a way to get the accountId off the user record so we can lookup what account they are tied to?

    ReplyDelete
    Replies
    1. Okay, the answer is yes, spelling is crucial AccountId get this.

      Delete
  4. Hi Amit,

    I created LWC using vscode but I unable to find the ".css" of particular LWC.
    Could you please tell where I can find ".css" of LWC ?
    Thanks ,
    Vicky

    ReplyDelete