Files
CMS-Client/utils/localStorageHandler.js
T

28 lines
771 B
JavaScript

// utils/localStorageHandler.js
import { getCookie, deleteCookie } from "cookies-next";
const handleLocalStorageChanges = () => {
const storedUserProfile = localStorage.getItem('cmc-profile');
const storedToken = getCookie('cmc-token');
window.onstorage = (e) => {
if (e.key === 'userProfile' && e.newValue !== storedUserProfile) {
// User profile data in 'localStorage' has changed
// Delete the 'userProfile' from 'localStorage'
localStorage.removeItem("cmc-profile");
// Delete the 'cmc-token' cookie
deleteCookie('cmc-token');
// Redirect to the login page
window.location.href = '/auth/login';
// Reload the page
window.location.reload();
}
};
};
export default handleLocalStorageChanges;