Skip to content

Broadcasting logout event in order to redirect other tabs to login page #1274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Acls from "./components/users/Acls";
import About from "./components/About";
import { useAppDispatch } from "./store";
import { fetchOcVersion, fetchUserInfo } from "./slices/userInfoSlice";
import { subscribeToAuthEvents } from "./utils/broadcastSync";

function App() {
const dispatch = useAppDispatch();
Expand All @@ -24,6 +25,9 @@ function App() {
// Load information about current opencast version on mount
dispatch(fetchOcVersion());

// Subscribe to the auth event to follow the login - logout events!
subscribeToAuthEvents();

// Add event listener for back button to check if we are still logged in
window.addEventListener("popstate", function (event) {
dispatch(fetchUserInfo());
Expand Down
11 changes: 7 additions & 4 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ import { HiTranslate } from "react-icons/hi";
import { IconContext } from "react-icons";
import ButtonLikeAnchor from "./shared/ButtonLikeAnchor";
import { ModalHandle } from "./shared/modals/Modal";
import { broadcastLogout } from "../utils/broadcastSync";

// References for detecting a click outside of the container of the dropdown menus
const containerLang = React.createRef<HTMLDivElement>();
const containerHelp = React.createRef<HTMLDivElement>();
const containerUser = React.createRef<HTMLDivElement>();
const containerNotify = React.createRef<HTMLDivElement>();

function logout() {
window.location.href = "/j_spring_security_logout";
}

/**
* Component that renders the header and the navigation in the upper right corner.
*/
Expand Down Expand Up @@ -417,6 +414,12 @@ const MenuHelp = ({

const MenuUser = () => {
const { t } = useTranslation();

const logout = () => {
// Here we broadcast logout, in order to redirect other tabs to login page!
broadcastLogout();
window.location.href = "/j_spring_security_logout";
}
return (
<ul className="dropdown-ul">
<li>
Expand Down
21 changes: 21 additions & 0 deletions src/utils/broadcastSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const bc = new BroadcastChannel('auth_channel');

export const broadcastLogout = () => {
bc.postMessage({ type: 'LOGOUT' });
};

export const subscribeToAuthEvents = () => {
bc.onmessage = (event) => {
if (event.data?.type === 'LOGOUT') {
performOnLogoutActions();
}
};
};

const performOnLogoutActions = () => {
// We need a tiny pause, in order to make sure the logout process has happened.
const intvl = setInterval(() => {
window.location.href = "/login.html";
clearInterval(intvl);
}, 750);
}
Loading