mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-13 11:23:50 +00:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { PropsWithChildren, useCallback, useContext } from 'react';
|
|
|
|
import { isLocalhost } from '../../../externals';
|
|
import { AppContext } from '../../context/AppContext';
|
|
|
|
import PinPage from './PinPage';
|
|
|
|
interface ProtectRouteProps {
|
|
permission: 'editor' | 'operator';
|
|
}
|
|
|
|
export default function ProtectRoute({ permission, children }: PropsWithChildren<ProtectRouteProps>) {
|
|
const { editorAuth, operatorAuth, validate } = useContext(AppContext);
|
|
|
|
const handleValidation = useCallback(
|
|
(pin: string) => {
|
|
return validate(pin, permission);
|
|
},
|
|
[permission, validate],
|
|
);
|
|
|
|
const hasRelevantAuth = () => {
|
|
if (permission === 'editor') {
|
|
return editorAuth;
|
|
}
|
|
if (permission === 'operator') {
|
|
return operatorAuth;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
if (isLocalhost || hasRelevantAuth()) {
|
|
// eslint-disable-next-line react/jsx-no-useless-fragment -- trying to make typescript happy
|
|
return <>{children}</>;
|
|
}
|
|
|
|
return <PinPage permission={permission} handleValidation={handleValidation} />;
|
|
}
|