Skip to content

Commit 20556f2

Browse files
committed
[Feat] Implement user authentication check and redirect in dashboard layout
1 parent 13edaab commit 20556f2

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

Frontend/app/dashboard/layout.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,38 @@ import MainModal from "@/components/modals/MainModal";
55
import GradientBackground from "@/components/ui/GradientBackground";
66
import React, { useEffect, useState } from "react";
77
import { useRouter, usePathname } from "next/navigation";
8+
import { authService } from "@/app/auth/authService";
89

910
export 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

Comments
 (0)