@@ -5,18 +5,38 @@ import MainModal from "@/components/modals/MainModal";
55import GradientBackground from "@/components/ui/GradientBackground" ;
66import React , { useEffect , useState } from "react" ;
77import { useRouter , usePathname } from "next/navigation" ;
8+ import { authService } from "@/app/auth/authService" ;
89
910export function WithLayoutLayout ( { children } : { children : React . ReactNode } ) {
1011 const [ showSidebar , setShowSidebar ] = useState ( false ) ;
1112 const router = useRouter ( ) ;
1213 const pathname = usePathname ( ) ;
14+ const [ isClient , setIsClient ] = useState ( false ) ;
1315
1416 useEffect ( ( ) => {
17+ setIsClient ( true ) ;
18+ } , [ ] ) ;
19+
20+ useEffect ( ( ) => {
21+ if ( ! isClient ) return ;
22+
23+ // Check if user is authenticated
24+ if ( ! authService . isAuthenticated ( ) ) {
25+ // Redirect to login page if not authenticated
26+ router . push ( "/auth/login" ) ;
27+ return ;
28+ }
29+
1530 // If the user is at the root of with-layout, redirect to home
1631 if ( pathname === "/dashboard" ) {
1732 router . push ( "/home" ) ;
1833 }
19- } , [ pathname , router ] ) ;
34+ } , [ isClient , pathname , router ] ) ;
35+
36+ // Don't render anything during authentication check to prevent flash of content
37+ if ( isClient && ! authService . isAuthenticated ( ) ) {
38+ return null ;
39+ }
2040
2141 return (
2242 < div className = "text-n500 relative z-10 h-dvh dark:text-n30" >
0 commit comments