Skip to content

Commit 0ef62f4

Browse files
authored
Implement broadcast synchronization for logout events and user authentication (#1274)
1 parent bd1a775 commit 0ef62f4

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/App.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Acls from "./components/users/Acls";
1515
import About from "./components/About";
1616
import { useAppDispatch } from "./store";
1717
import { fetchOcVersion, fetchUserInfo } from "./slices/userInfoSlice";
18+
import { subscribeToAuthEvents } from "./utils/broadcastSync";
1819

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

28+
// Subscribe to the auth event to follow the login - logout events!
29+
subscribeToAuthEvents();
30+
2731
// Add event listener for back button to check if we are still logged in
2832
window.addEventListener("popstate", function (event) {
2933
dispatch(fetchUserInfo());

src/components/Header.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ import { HiTranslate } from "react-icons/hi";
2424
import { IconContext } from "react-icons";
2525
import ButtonLikeAnchor from "./shared/ButtonLikeAnchor";
2626
import { ModalHandle } from "./shared/modals/Modal";
27+
import { broadcastLogout } from "../utils/broadcastSync";
2728

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

34-
function logout() {
35-
window.location.href = "/j_spring_security_logout";
36-
}
37-
3835
/**
3936
* Component that renders the header and the navigation in the upper right corner.
4037
*/
@@ -417,6 +414,12 @@ const MenuHelp = ({
417414

418415
const MenuUser = () => {
419416
const { t } = useTranslation();
417+
418+
const logout = () => {
419+
// Here we broadcast logout, in order to redirect other tabs to login page!
420+
broadcastLogout();
421+
window.location.href = "/j_spring_security_logout";
422+
}
420423
return (
421424
<ul className="dropdown-ul">
422425
<li>

src/utils/broadcastSync.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const bc = new BroadcastChannel('auth_channel');
2+
3+
export const broadcastLogout = () => {
4+
bc.postMessage({ type: 'LOGOUT' });
5+
};
6+
7+
export const subscribeToAuthEvents = () => {
8+
bc.onmessage = (event) => {
9+
if (event.data?.type === 'LOGOUT') {
10+
performOnLogoutActions();
11+
}
12+
};
13+
};
14+
15+
const performOnLogoutActions = () => {
16+
// We need a tiny pause, in order to make sure the logout process has happened.
17+
const intvl = setInterval(() => {
18+
window.location.href = "/login.html";
19+
clearInterval(intvl);
20+
}, 750);
21+
}

0 commit comments

Comments
 (0)