From 4ef0aa47a4e10112055309be9a625c37050985fd Mon Sep 17 00:00:00 2001 From: Prakash Maity Date: Wed, 7 Aug 2024 17:18:59 +0530 Subject: [PATCH] auth guard changes --- src/app/(dashboard)/layout.tsx | 6 +++- src/components/wrapper/dashboardWrapper.tsx | 20 ++++++------- src/hoc/authGuard/authGuard.tsx | 31 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/hoc/authGuard/authGuard.tsx diff --git a/src/app/(dashboard)/layout.tsx b/src/app/(dashboard)/layout.tsx index 364a871..f823e4b 100644 --- a/src/app/(dashboard)/layout.tsx +++ b/src/app/(dashboard)/layout.tsx @@ -1,10 +1,14 @@ +"use client" import DashboardWrapper from "@/components/wrapper/dashboardWrapper"; +import AuthGuard from "@/hoc/authGuard/authGuard"; interface RootLayoutProps { children: React.ReactNode; } -export default function RootLayout(props: RootLayoutProps): JSX.Element { +function RootLayout(props: RootLayoutProps): JSX.Element { const { children } = props; return {children}; } + +export default AuthGuard(RootLayout); \ No newline at end of file diff --git a/src/components/wrapper/dashboardWrapper.tsx b/src/components/wrapper/dashboardWrapper.tsx index 0f45def..b3c6176 100644 --- a/src/components/wrapper/dashboardWrapper.tsx +++ b/src/components/wrapper/dashboardWrapper.tsx @@ -1,5 +1,4 @@ "use client"; // This ensures the file is a client component - import * as React from "react"; import AppBar, { AppBarProps } from "@mui/material/AppBar"; import Box from "@mui/material/Box"; @@ -12,7 +11,6 @@ import ListItem from "@mui/material/ListItem"; import ListItemButton from "@mui/material/ListItemButton"; import ListItemIcon from "@mui/material/ListItemIcon"; import ListItemText from "@mui/material/ListItemText"; -import MailIcon from "@mui/icons-material/Mail"; import MenuIcon from "@mui/icons-material/Menu"; import Toolbar from "@mui/material/Toolbar"; import Typography from "@mui/material/Typography"; @@ -22,6 +20,7 @@ import styles from "./DashboardWrapper.module.scss"; import styled from "@emotion/styled"; import { BUILDING, GEAR } from "@/utilities/svgConstant"; import LogoutIcon from "@mui/icons-material/Logout"; +import { useRouter } from "next/navigation"; const drawerWidth = 260; @@ -75,7 +74,7 @@ export default function DashboardWrapper(props: Props) { const { children: Children } = props; const [mobileOpen, setMobileOpen] = useState(false); const [isClosing, setIsClosing] = useState(false); - + const router = useRouter() const handleDrawerClose = () => { setIsClosing(true); setMobileOpen(false); @@ -125,6 +124,12 @@ export default function DashboardWrapper(props: Props) { const container = window !== undefined ? () => window().document.body : undefined; + const logoutHandler = () => { + localStorage.removeItem('token'); + localStorage.removeItem('refreshToken'); + router.push('/log-in'); + } + return ( - {/* */} - + diff --git a/src/hoc/authGuard/authGuard.tsx b/src/hoc/authGuard/authGuard.tsx new file mode 100644 index 0000000..e575a16 --- /dev/null +++ b/src/hoc/authGuard/authGuard.tsx @@ -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 ; + }; + + // 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'; +}