|
| 1 | +/*! |
| 2 | + * Copyright 2026 - Swiss Data Science Center (SDSC) |
| 3 | + * A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 4 | + * Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import type { Location, NavigateFunction } from "react-router"; |
| 20 | + |
| 21 | +import { NOTIFICATION_TOPICS } from "~/notifications/Notifications.constants"; |
| 22 | +import type { NotificationsManager } from "~/notifications/notifications.types"; |
| 23 | +import { |
| 24 | + LOGOUT_EVENT_TIMEOUT, |
| 25 | + RENKU_QUERY_PARAMS, |
| 26 | + RENKU_USER_SIGNED_IN_COOKIE, |
| 27 | + RENKU_USER_SIGNED_IN_COOKIE_TTL, |
| 28 | +} from "./authentication.constants"; |
| 29 | + |
| 30 | +interface HandleLoginParamsArgs { |
| 31 | + location: Location<unknown>; |
| 32 | + navigate: NavigateFunction; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Manages communication of login/logout events between tabs. It uses localStorage to communicate |
| 37 | + * the events between tabs, and uses sessionStorage to remember an event after a refresh within a tab. |
| 38 | + * |
| 39 | + * Remove renku login parameters and set localStorage object |
| 40 | + */ |
| 41 | +export function handleLoginParams({ |
| 42 | + location, |
| 43 | + navigate, |
| 44 | +}: HandleLoginParamsArgs): void { |
| 45 | + // Check if user has just logged in |
| 46 | + const queryParams = new URLSearchParams(location.search); |
| 47 | + if ( |
| 48 | + queryParams.get(RENKU_QUERY_PARAMS.login) === RENKU_QUERY_PARAMS.loginValue |
| 49 | + ) { |
| 50 | + // delete the login param |
| 51 | + queryParams.delete(RENKU_QUERY_PARAMS.login); |
| 52 | + navigate({ search: queryParams.toString() }, { replace: true }); |
| 53 | + |
| 54 | + // save the login time to localStorage to allow other tabs to handle the event |
| 55 | + localStorage.setItem(RENKU_QUERY_PARAMS.login, Date.now().toString()); |
| 56 | + |
| 57 | + // save the login state in a client-side cookie |
| 58 | + if (window.cookieStore) { |
| 59 | + window.cookieStore.set({ |
| 60 | + name: RENKU_USER_SIGNED_IN_COOKIE, |
| 61 | + value: "1", |
| 62 | + expires: Date.now() + RENKU_USER_SIGNED_IN_COOKIE_TTL, |
| 63 | + sameSite: "strict", |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Set up event listener fol localStorage authentication events. There should be only one listener per browser tab. |
| 71 | + * |
| 72 | + * Returns a cleanup function. |
| 73 | + */ |
| 74 | +export function setupListener(): () => void { |
| 75 | + function listener(event: StorageEvent): void { |
| 76 | + if (event.key === RENKU_QUERY_PARAMS.logout) { |
| 77 | + setTimeout(() => { |
| 78 | + sessionStorage.setItem( |
| 79 | + RENKU_QUERY_PARAMS.logout, |
| 80 | + Date.now().toString() |
| 81 | + ); |
| 82 | + window.location.reload(); |
| 83 | + }, LOGOUT_EVENT_TIMEOUT); |
| 84 | + } else if (event.key === RENKU_QUERY_PARAMS.login) { |
| 85 | + sessionStorage.setItem(RENKU_QUERY_PARAMS.login, Date.now().toString()); |
| 86 | + window.location.reload(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + window.addEventListener("storage", listener); |
| 91 | + return () => { |
| 92 | + window.removeEventListener("storage", listener); |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Set up event listener fol localStorage authentication events. This should be called once per browser tab. |
| 98 | + * |
| 99 | + * @param {object} notifications - notification manager object. |
| 100 | + * |
| 101 | + * Returns a cleanup function. |
| 102 | + */ |
| 103 | +export function triggerNotifications(notifications: NotificationsManager) { |
| 104 | + // Check login |
| 105 | + const login = sessionStorage.getItem(RENKU_QUERY_PARAMS.login); |
| 106 | + if (login) { |
| 107 | + sessionStorage.removeItem(RENKU_QUERY_PARAMS.login); |
| 108 | + notifications.addSuccess( |
| 109 | + NOTIFICATION_TOPICS.AUTHENTICATION, |
| 110 | + "The page was refreshed because you recently logged in on a different tab." |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + // Check logout |
| 115 | + const logout = sessionStorage.getItem(RENKU_QUERY_PARAMS.logout); |
| 116 | + if (logout) { |
| 117 | + sessionStorage.removeItem(RENKU_QUERY_PARAMS.logout); |
| 118 | + notifications.addWarning( |
| 119 | + NOTIFICATION_TOPICS.AUTHENTICATION, |
| 120 | + "The page was refreshed because you recently logged out on a different tab." |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
| 124 | +/** |
| 125 | + * Invoke whenever then logout process has been triggered. |
| 126 | + */ |
| 127 | +export function notifyLogout(): void { |
| 128 | + localStorage.setItem(RENKU_QUERY_PARAMS.logout, Date.now().toString()); |
| 129 | + // Manual logout: remove the `renku_user_signed_in` cookie |
| 130 | + if (window.cookieStore) { |
| 131 | + window.cookieStore.delete(RENKU_USER_SIGNED_IN_COOKIE); |
| 132 | + } |
| 133 | +} |
0 commit comments