Separate Client Portal & Dashboard
This commit is contained in:
38
frontend/dashboard/src/guards/AuthGuard.tsx
Normal file
38
frontend/dashboard/src/guards/AuthGuard.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useState, ReactNode } from 'react';
|
||||
import { Navigate, useLocation } from 'react-router-dom';
|
||||
// hooks
|
||||
import useAuth from '../hooks/useAuth';
|
||||
// pages
|
||||
import Login from '../pages/auth/Login';
|
||||
// components
|
||||
import LoadingScreen from '../components/LoadingScreen';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type AuthGuardProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
export default function AuthGuard({ children }: AuthGuardProps) {
|
||||
const { isAuthenticated, isInitialized } = useAuth();
|
||||
const { pathname } = useLocation();
|
||||
const [requestedLocation, setRequestedLocation] = useState<string | null>(null);
|
||||
|
||||
if (!isInitialized) {
|
||||
return <LoadingScreen />;
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
if (pathname !== requestedLocation) {
|
||||
setRequestedLocation(pathname);
|
||||
}
|
||||
return <Login />;
|
||||
}
|
||||
|
||||
if (requestedLocation && pathname !== requestedLocation) {
|
||||
setRequestedLocation(null);
|
||||
return <Navigate to={requestedLocation} />;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
Reference in New Issue
Block a user