@@ -3,20 +3,15 @@ import {
33 Link ,
44 Outlet ,
55 redirect ,
6+ useNavigate ,
67} from "@tanstack/react-router" ;
78import { CogIcon , LibraryIcon , VideoIcon , ActivityIcon } from "lucide-react" ;
89import { useAuth } from "@/hooks/useAuth" ;
10+ import { useEffect } from "react" ;
911
1012export const Route = createFileRoute ( "/settings" ) ( {
1113 component : RouteComponent ,
12- beforeLoad : ( { location, context } ) => {
13- // Block unauthenticated users and guests from accessing settings
14- const user = ( context as { auth ?: { user ?: { role ?: string } } } ) ?. auth
15- ?. user ;
16- if ( ! user || user ?. role === "GUEST" ) {
17- throw redirect ( { to : "/login" } ) ;
18- }
19-
14+ beforeLoad : ( { location } ) => {
2015 // If we're at the exact settings route, redirect to libraries
2116 if ( location . pathname === "/settings" ) {
2217 throw redirect ( { to : "/settings/libraries" } ) ;
@@ -25,9 +20,17 @@ export const Route = createFileRoute("/settings")({
2520} ) ;
2621
2722function RouteComponent ( ) {
28- const { user } = useAuth ( ) ;
23+ const { user, isAuthenticated, isLoading } = useAuth ( ) ;
24+ const navigate = useNavigate ( ) ;
2925 const isAdmin = user ?. role === "ADMIN" ;
3026
27+ // Redirect unauthenticated users or guests
28+ useEffect ( ( ) => {
29+ if ( ! isLoading && ( ! isAuthenticated || user ?. role === "GUEST" ) ) {
30+ navigate ( { to : "/" } ) ;
31+ }
32+ } , [ isAuthenticated , user ?. role , isLoading , navigate ] ) ;
33+
3134 return (
3235 < div className = "pt-[138px] px-4 max-w-7xl mx-auto flex gap-4 h-[calc(100vh-138px)]" >
3336 < nav className = "max-w-sm w-full bg-white/10 backdrop-blur-lg rounded-xl p-2 space-y-2" >
0 commit comments