Saturday 16 January 2021

Get Information About the Current User

To get information about the current user, use the @salesforce/user scoped module.

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

  • property—The supported properties are Id, which is the user’s ID, and isGuest, which is a boolean value indicating whether the user is a community guest user. Use the isGuest property to check whether or not the user is authenticated.

This sample code imports the current user ID and assigns it to the userId property to provide access in the HTML template.

html

<template>
    <lightning-card title="currentuserId" icon-name="custom:custom19">
        <div class="slds-m-around_medium">
            <p>User Id:</p>
            {userId}
        </div>
    </lightning-card>
</template>

js

import { LightningElement } from 'lwc';
import Id from '@salesforce/user/Id';
export default class CurrentUser extends LightningElement {
    // Assign to variable
    userId = Id;
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>50.0</apiVersion>
    <isExposed>true</isExposed> 
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>

</LightningComponentBundle>

No comments:

Post a Comment