2

There are multiple posts about using URLs of this nature:

.../secur/logout.jsp?retUrl=...

e.g. how to logout from lightning community to logout. And for LWC, there are these functions:

import getLogoutUrl from '@salesforce/apex/applauncher.IdentityHeaderController.getLogoutUrl';
import getLoginUrl from '@salesforce/apex/system.Network.getLoginUrl';

mentioned in Create a Logout Link Component.

But after several hours of trying many permutations, I couldn't get a solution that both did the logout and left the user back at the experience login rather than the general Salesforce login. The problem might be this known issue Logout URL in community is not working if there is only one authentication provider login option.

Is there a simple workaround for this problem?

2 Answers 2

4

This is the workaround I found using the very convenient fetch API to do the logout part:

async logout() {

    // Logout
    const logoutUrl = await getLogoutUrl();
    await fetch(logoutUrl);

    // Login page (or could navigate to some other URL)
    this[NavigationMixin.Navigate]({
        type: 'comm__loginPage',
        attributes: { actionName: 'login' }
    });
}

I found that getLoginUrl did nothing useful.

1
  • Epic, clean and functional!
    – veritascs
    Commented Oct 12, 2023 at 15:56
0

Logout should be done using the named page with the logout action, it's a lot like the example above but I would suggest to do this:

import {LightningElement} from "lwc";
import {NavigationMixin} from "lightning/navigation";

const LOGOUTPAGEREF = {
    type: "comm__loginPage",
    attributes: {
        actionName: "logout"
    }
};

export default class ComLogoutButton extends NavigationMixin(LightningElement) {
    logout() {
        this[NavigationMixin.Navigate](LOGOUTPAGEREF);
    }
}

the template can look like this:

<template>
    <a onclick={logout}>
        <lightning-icon icon-name="utility:logout" alternative-text="Logout"> </lightning-icon>
    </a>
</template>

for everyone else's convenience I have created a repo with a quick deploy option here: https://github.com/Henk3000/communityLogoutButton

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .