Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f431e1e8d6 | |||
| d7fea3c760 | |||
| 7634aaa225 |
@@ -107,13 +107,12 @@ 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;
|
||||||
localStorage.setItem("cmc-profile", JSON.stringify(userProfileData));
|
dispatch({ type: 'SET_USER_PROFILE', payload: userProfileData });
|
||||||
|
|
||||||
dispatch({ type: "SET_USER_PROFILE", payload: userProfileData });
|
|
||||||
|
|
||||||
enqueueSnackbar("Login Successful", {
|
enqueueSnackbar("Login Successful", {
|
||||||
variant: "success",
|
variant: "success",
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ 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) => {
|
||||||
@@ -104,7 +102,7 @@ const Profile = () => {
|
|||||||
fontWeight: "500",
|
fontWeight: "500",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{`${userProfile?.firstname} ${userProfile?.lastname}`}
|
Adison Jeck
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
|
|||||||
+1
-2
@@ -31,9 +31,8 @@ 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
|
||||||
|
|||||||
@@ -8,9 +8,8 @@ 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 and local storage
|
// Remove the cookie
|
||||||
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);
|
||||||
|
|||||||
@@ -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;
|
|
||||||
Reference in New Issue
Block a user