mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-12-06 21:44:13 +03:00
* update config for roles and add validator * ensure admin and viewer are never overridden * add class method to user to retrieve all allowed cameras * enforce config roles in auth api endpoints * add camera access api dependency functions * protect review endpoints * protect preview endpoints * rename param name for better fastapi injection matching * remove unneeded * protect export endpoints * protect event endpoints * protect media endpoints * update auth hook for allowed cameras * update default app view * ensure anonymous user always returns all cameras * limit cameras in explore * cameras is already a list * limit cameras in review/history * limit cameras in live view * limit cameras in camera groups * only show face library and classification in sidebar for admin * remove check in delete reviews since admin role is required, no need to check camera access. fixes failing test * pass request with camera access for tests * more async * camera access tests * fix proxy auth tests * allowed cameras for review tests * combine event tests and refactor for camera access * fix post validation for roles * don't limit roles in create user dialog * fix triggers endpoints no need to run require camera access dep since the required role is admin * fix type * create and edit role dialogs * delete role dialog * fix role change dialog * update settings view for roles * i18n changes * minor spacing tweaks * docs * use badges and camera name label component * clarify docs * display all cameras badge for admin and viewer * i18n fix * use validator to prevent reserved and empty roles from being assigned * split users and roles into separate tabs in settings * tweak docs * clarify docs * change icon * don't memoize roles always recalculate on component render
41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { useContext } from "react";
|
||
import { Navigate, Outlet } from "react-router-dom";
|
||
import { AuthContext } from "@/context/auth-context";
|
||
import ActivityIndicator from "../indicators/activity-indicator";
|
||
|
||
export default function ProtectedRoute({
|
||
requiredRoles,
|
||
}: {
|
||
requiredRoles: string[];
|
||
}) {
|
||
const { auth } = useContext(AuthContext);
|
||
|
||
if (auth.isLoading) {
|
||
return (
|
||
<ActivityIndicator className="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2" />
|
||
);
|
||
}
|
||
|
||
// Unauthenticated mode
|
||
if (!auth.isAuthenticated) {
|
||
return <Outlet />;
|
||
}
|
||
|
||
// Authenticated mode (8971): require login
|
||
if (!auth.user) {
|
||
return <Navigate to="/login" replace />;
|
||
}
|
||
|
||
// If role is null (shouldn’t happen if isAuthenticated, but type safety), fallback
|
||
// though isAuthenticated should catch this
|
||
if (auth.user.role === null) {
|
||
return <Outlet />;
|
||
}
|
||
|
||
if (!requiredRoles.includes(auth.user.role)) {
|
||
return <Navigate to="/unauthorized" replace />;
|
||
}
|
||
|
||
return <Outlet />;
|
||
}
|