How to check current user is guest user in Lightning Web Component?

Are you working on building communities portal using Lightning web component? You might want to check current user is community guest user dynamically and effectively. Lightning web component provides you a way to get user information without apex. To get information about the current user, use the @salesforce/user scoped module.

The syntax to import the @salesforce/user 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.

If you are looking for getting the current user id, check this:- GETTING CURRENT USER INFORMATION IN LIGHTNING WEB COMPONENT WITHOUT CALLING APEX METHOD

Lets create an component where we are going check the user authentication i.e Current user is guest user or not in Lightning web component.

How to check current user is community guest user in Lightning Web Component?

The component name is checkGuestUserInLWC. The html file of the this component is having below code. It has a lightning-card to show card with title in Lightning web component. It also has property with name isGuestUser to show the Boolean value to show the user is guest user or not.

checkGuestUserInLWC.html

<template>
    <lightning-card title="Check Guest User In Lightning Web Component" icon-name="custom:custom19">
        The user is Guset User - {isGuestUser}
    </lightning-card>
</template>

checkGuestUserInLWC.js

The js file of the component has imported the @salesforce/user scoped module and passed isGuest as property which returns the boolean value after checking the current user is guest user or not.

import { LightningElement } from 'lwc';
import isguest from '@salesforce/user/isGuest';
export default class CheckGuestUserInLWC extends LightningElement {
    // Expose the value of @salesforce/user/isGuest in the template.
    isGuestUser = isguest;
}

checkGuestUserInLWC.js-meta.xml

To expose the component to be used with Salesforce in home page, record page, application page, community pages, we have used the meta file to provide the target value.

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

Demo – How to check current user is community guest user in Lightning Web Component?

Deploy the component to Salesforce and place it on a lightning page. You can see the component as below. The value is false because we are checking it in Salesforce as a internal user.

How to check current user is community guest user in Lightning Web Component?

4 comments

  1. Hi Sanket – Do you know if there is a similarly simple way to do this (check if a user is a guest user) in an Aura component?

Leave a Reply