Current User in LWC

Current User ID in LWC

There’s additional property which can be imported - isGuest. isGuest s a boolean value indicating whether the user is a guest user and it should be used in the Experience Builder sites.

Did you know that there’s a simple way to retrieve current user Id in LWC?

Instead of passing it from APEX, you can simply import it in a way which is presented below:

javascript
import { LightningElement } from 'lwc';
import userId from '@salesforce/user/Id';

export default class CurrentUser extends LightningElement {
    // Expose to template
    userId = userId;
}

There’s additional property which can be imported - isGuest. isGuest s a boolean value indicating whether the user is a guest user and it should be used in the Experience Builder sites.

javascript
import { LightningElement } from 'lwc';
import userId from '@salesforce/user/Id';
import isGuest from '@salesforce/user/isGuest';

export default class CurrentUser extends LightningElement {
    // Expose to template
    userId = userId;
    isGuest = isGuest;
}

Those two properties might be especially useful while working on components for communities.

Text and code were extracted from the original slide. Plain-text version of the whole catalog