3 Commits

5 changed files with 5 additions and 37 deletions
+2 -3
View File
@@ -107,13 +107,12 @@ const SignInForm = () => {
httpOnly: true, // Make the cookie accessible only via HTTP (recommended for security)
};
// Set the login token in a cookie
await setCookie("cmc-token", res.token);
const userProfileData = res.profile;
localStorage.setItem("cmc-profile", JSON.stringify(userProfileData));
dispatch({ type: "SET_USER_PROFILE", payload: userProfileData });
dispatch({ type: 'SET_USER_PROFILE', payload: userProfileData });
enqueueSnackbar("Login Successful", {
variant: "success",
+1 -3
View File
@@ -23,8 +23,6 @@ const Profile = () => {
const { state } = useUserProfile();
const userProfile = state.userProfile;
console.log(userProfile)
const [anchorEl, setAnchorEl] = React.useState(null);
const open = Boolean(anchorEl);
const handleClick = (event) => {
@@ -104,7 +102,7 @@ const Profile = () => {
fontWeight: "500",
}}
>
{`${userProfile?.firstname} ${userProfile?.lastname}`}
Adison Jeck
</Typography>
</Box>
</MenuItem>
+1 -2
View File
@@ -31,9 +31,8 @@ function MyApp({ Component, pageProps }) {
}
};
// Attach the event handler when the component mounts
useEffect(() => {
// Attach the event handler when the component mounts
window.onpopstate = handlePopState;
// Clean up the event handler when the component unmounts
+1 -2
View File
@@ -8,9 +8,8 @@ import { deleteCookie } from "cookies-next";
export default function Logout() {
const router = useRouter();
const handleLogout = () => {
// Remove the cookie and local storage
// Remove the cookie
deleteCookie("cmc-token");
localStorage.removeItem("cmc-profile");
// Use replaceState to replace the current URL with the root URL
window.history.replaceState({}, document.title, window.location.href);
-27
View File
@@ -1,27 +0,0 @@
// 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;