✨ private route
This commit is contained in:
parent
c3001d8b8f
commit
2261af9848
@ -1,3 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import ContentAnimation from '@/components/layouts/content-animation';
|
||||
import Footer from '@/components/layouts/footer';
|
||||
import Header from '@/components/layouts/header';
|
||||
@ -7,8 +9,11 @@ import ScrollToTop from '@/components/layouts/scroll-to-top';
|
||||
import Setting from '@/components/layouts/setting';
|
||||
import Sidebar from '@/components/layouts/sidebar';
|
||||
import Portals from '@/components/portals';
|
||||
import withAuth from '@/hoc/withAuth';
|
||||
import { FC } from 'react';
|
||||
|
||||
const DefaultLayout: FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
|
||||
export default function DefaultLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<>
|
||||
{/* BEGIN MAIN CONTAINER */}
|
||||
@ -39,3 +44,5 @@ export default function DefaultLayout({ children }: { children: React.ReactNode
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default withAuth(DefaultLayout);
|
||||
|
27
hoc/withAuth.tsx
Normal file
27
hoc/withAuth.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
const withAuth = (WrappedComponent: React.FC) => {
|
||||
return (props: any) => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false)
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem("token")
|
||||
|
||||
if (!token) {
|
||||
router.replace("/login") // Redirect to login if no token
|
||||
} else {
|
||||
setIsAuthenticated(true)
|
||||
}
|
||||
}, [])
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return null // Avoid rendering until auth check is done
|
||||
}
|
||||
|
||||
return <WrappedComponent {...props} />
|
||||
};
|
||||
};
|
||||
|
||||
export default withAuth
|
Loading…
x
Reference in New Issue
Block a user