auth guard changes
This commit is contained in:
parent
0de91ae643
commit
4ef0aa47a4
@ -1,10 +1,14 @@
|
|||||||
|
"use client"
|
||||||
import DashboardWrapper from "@/components/wrapper/dashboardWrapper";
|
import DashboardWrapper from "@/components/wrapper/dashboardWrapper";
|
||||||
|
import AuthGuard from "@/hoc/authGuard/authGuard";
|
||||||
|
|
||||||
interface RootLayoutProps {
|
interface RootLayoutProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function RootLayout(props: RootLayoutProps): JSX.Element {
|
function RootLayout(props: RootLayoutProps): JSX.Element {
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
return <DashboardWrapper>{children}</DashboardWrapper>;
|
return <DashboardWrapper>{children}</DashboardWrapper>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default AuthGuard(RootLayout);
|
@ -1,5 +1,4 @@
|
|||||||
"use client"; // This ensures the file is a client component
|
"use client"; // This ensures the file is a client component
|
||||||
|
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import AppBar, { AppBarProps } from "@mui/material/AppBar";
|
import AppBar, { AppBarProps } from "@mui/material/AppBar";
|
||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
@ -12,7 +11,6 @@ import ListItem from "@mui/material/ListItem";
|
|||||||
import ListItemButton from "@mui/material/ListItemButton";
|
import ListItemButton from "@mui/material/ListItemButton";
|
||||||
import ListItemIcon from "@mui/material/ListItemIcon";
|
import ListItemIcon from "@mui/material/ListItemIcon";
|
||||||
import ListItemText from "@mui/material/ListItemText";
|
import ListItemText from "@mui/material/ListItemText";
|
||||||
import MailIcon from "@mui/icons-material/Mail";
|
|
||||||
import MenuIcon from "@mui/icons-material/Menu";
|
import MenuIcon from "@mui/icons-material/Menu";
|
||||||
import Toolbar from "@mui/material/Toolbar";
|
import Toolbar from "@mui/material/Toolbar";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
@ -22,6 +20,7 @@ import styles from "./DashboardWrapper.module.scss";
|
|||||||
import styled from "@emotion/styled";
|
import styled from "@emotion/styled";
|
||||||
import { BUILDING, GEAR } from "@/utilities/svgConstant";
|
import { BUILDING, GEAR } from "@/utilities/svgConstant";
|
||||||
import LogoutIcon from "@mui/icons-material/Logout";
|
import LogoutIcon from "@mui/icons-material/Logout";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
const drawerWidth = 260;
|
const drawerWidth = 260;
|
||||||
|
|
||||||
@ -75,7 +74,7 @@ export default function DashboardWrapper(props: Props) {
|
|||||||
const { children: Children } = props;
|
const { children: Children } = props;
|
||||||
const [mobileOpen, setMobileOpen] = useState(false);
|
const [mobileOpen, setMobileOpen] = useState(false);
|
||||||
const [isClosing, setIsClosing] = useState(false);
|
const [isClosing, setIsClosing] = useState(false);
|
||||||
|
const router = useRouter()
|
||||||
const handleDrawerClose = () => {
|
const handleDrawerClose = () => {
|
||||||
setIsClosing(true);
|
setIsClosing(true);
|
||||||
setMobileOpen(false);
|
setMobileOpen(false);
|
||||||
@ -125,6 +124,12 @@ export default function DashboardWrapper(props: Props) {
|
|||||||
const container =
|
const container =
|
||||||
window !== undefined ? () => window().document.body : undefined;
|
window !== undefined ? () => window().document.body : undefined;
|
||||||
|
|
||||||
|
const logoutHandler = () => {
|
||||||
|
localStorage.removeItem('token');
|
||||||
|
localStorage.removeItem('refreshToken');
|
||||||
|
router.push('/log-in');
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
sx={{ display: "flex" }}
|
sx={{ display: "flex" }}
|
||||||
@ -178,15 +183,8 @@ export default function DashboardWrapper(props: Props) {
|
|||||||
alt="Remy Sharp"
|
alt="Remy Sharp"
|
||||||
src="/static/images/avatar/1.jpg"
|
src="/static/images/avatar/1.jpg"
|
||||||
/>
|
/>
|
||||||
{/* <GEAR
|
|
||||||
sx={{
|
|
||||||
color: "var(--primary_Active_text)",
|
|
||||||
fontSize: "1.8rem",
|
|
||||||
cursor: "pointer",
|
|
||||||
}}
|
|
||||||
/> */}
|
|
||||||
<IconButton aria-label="log out" size="medium" color="error">
|
<IconButton aria-label="log out" size="medium" color="error">
|
||||||
<LogoutIcon fontSize="small" />
|
<LogoutIcon fontSize="small" onClick={logoutHandler} />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
31
src/hoc/authGuard/authGuard.tsx
Normal file
31
src/hoc/authGuard/authGuard.tsx
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
"use client"
|
||||||
|
// hoc/withAuth.tsx
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
const AuthGuard = (WrappedComponent: any) => {
|
||||||
|
const AuthGuardComponent = (props: any) => {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
if (!token) {
|
||||||
|
router.push('/log-in');
|
||||||
|
}
|
||||||
|
}, [router]);
|
||||||
|
|
||||||
|
return <WrappedComponent {...props} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set display name for the component
|
||||||
|
AuthGuardComponent.displayName = `AuthGuard(${getDisplayName(WrappedComponent)})`;
|
||||||
|
|
||||||
|
return AuthGuardComponent;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AuthGuard;
|
||||||
|
|
||||||
|
// Helper function to get the display name of a component
|
||||||
|
function getDisplayName(WrappedComponent: any) {
|
||||||
|
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user