1 Commits

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