Added profile data but clears after refresh

This commit is contained in:
2023-10-24 01:25:12 -07:00
parent e0fb79f2c9
commit 07a40ee808
5 changed files with 37 additions and 5 deletions
+27
View File
@@ -0,0 +1,27 @@
// 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;