direct-client logout when multiple tabs open and redirects to login page.

20 views
Skip to first unread message

Kaushal Senevirathne

unread,
Feb 10, 2025, 12:56:01 AMFeb 10
to Pac4j development mailing list
Hi,

I'm using DirectBearerAuthClient my app creates cookie from JWT token created by keycloak (hybrid client).  I have open multiple tabs of my browser client. I need to logout from all automatically from all other my apps tab. when I logout from single tab of my browser client. how achieve this. using play-pac4j 6

 thanks 

Nicolas Crittin

unread,
Feb 10, 2025, 2:27:30 AMFeb 10
to Pac4j development mailing list
Hi,

When you logout, all tabs should also be logged out too since cookies are common to all tabs, but they are not refreshed.

In order to force refresh, you can use the browser BroadcastChannel feature. The idea is to register all tab on a BroadcastChannel whose name is the sessionId:

var myChannel = new BroadcastChannel("my-session-id");
myChannel.addEventListener("message", onMessage);

The " onMessage" function performs a redirection to a page that info that user has been logged out:

function onMessage(ev) {
    if (ev?.data?.message == 'logout') {
        window.open("url-to/disconnected.jsp", "_self");
    }
}

Now, when a tab logs out, it can simply post the "logout" message, then all tab that are registered on same channel will execute the code above.

function logout() {
    // do logout stuff
    // ...

    // broadcast logout message
    if (myChannel) {
        myChannel.postMessage({ message: 'logout' });
Reply all
Reply to author
Forward
0 new messages