Custom Logout in Salesforce Community

Hello devs,
Today we will focus on Salesforce Community Cloud. I would like to show you how we can create a custom logout button, which allows you to use it in custom navigation or custom components.
To achieve this goal, we will use the existing salesforce event listener, and we will create simple Aura and LWC components.
Let’s get started!

Custom logout button example
Custom Logout Button

Custom Logout Solution Background

In your community, you can find a js file called logoutHandler.js.
You can open it using chrome dev tools.
As you can see on the screens below, when the community is rendered, there is an event listener on force:logout event.
What we need to do is only fire that event on your own component!
Based on those assumptions we can develop our code.

logoutHandler.js
render logic
logoutHandler.js
handleForceLogout logic

Code | Solution

Aura will be used as a wrapper and a place to fire our event.
$A.get(“e.force:logout”).fire();
Unfortunately, we cannot fire it directly from Lightning Web Component.
Our LWC will contain the only logout appearance.

Lightning Web Component

customLogoutButton.html

<template>
  <lightning-button
    label="Logout"
    title="Logout"
    icon-position="right"
    icon-name="utility:logout"
    onclick={handleLogout}
  ></lightning-button>
</template>

customLogoutButton.js

import { LightningElement } from 'lwc';

export default class CustomLogoutButton extends LightningElement {

    handleLogout() {
        this.dispatchEvent(new CustomEvent('logout'));
    }
}

Aura Wrapper

customLogoutButtonWrapper.cmp

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global">
    <c:customLogoutButton onlogout="{!c.handleLogout}"/>
</aura:component>	

customLogoutButtonWrapperController.js

({
    handleLogout : function(component, event, helper) {
        $A.get("e.force:logout").fire();
    }
})

That’s all that I have for you today!
Enjoy!

Was it helpful? Check out our other great articles here.

References

5 2 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Custom Logout in Salesforce Community – SalesforceProfs […]

Close Menu
1
0
Would love your thoughts, please comment.x
()
x