mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-09-14 14:14:09 +00:00
15 lines
544 B
TypeScript
15 lines
544 B
TypeScript
import { memo, useContext } from 'react';
|
|
import { Navigate, Outlet } from 'react-router';
|
|
|
|
import { AuthenticatedContext } from 'contexts/authentication';
|
|
|
|
// Layout-route guard: renders nested admin routes only for admins, otherwise
|
|
// redirects home. Must be used inside the authenticated route subtree so that
|
|
// AuthenticatedContext (and `me`) is available.
|
|
const RequireAdmin = () => {
|
|
const { me } = useContext(AuthenticatedContext);
|
|
return me.admin ? <Outlet /> : <Navigate replace to="/" />;
|
|
};
|
|
|
|
export default memo(RequireAdmin);
|