Skip to content

Commit

Permalink
Redirect user away from register patient route if first responder
Browse files Browse the repository at this point in the history
  • Loading branch information
samau3 committed Sep 18, 2024
1 parent 136db7c commit 5aa70dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 33 additions & 1 deletion client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ function Redirect({ isLoading, isLoggedIn, isLoggedInRequired }) {

Redirect.propTypes = RedirectProps;

const ProtectedRouteProps = {
role: PropTypes.string.isRequired,
children: PropTypes.element.isRequired,
};

/**
* Protect route elements that don't allow for FIRST_RESPONDER role
* @param {PropTypes.InferProps<typeof ProtectedRouteProps>} props
* @returns {React.ReactElement}
*/
function ProtectedRoute({ role, children }) {
const navigate = useNavigate();
useEffect(() => {
if (role === 'FIRST_RESPONDER') {
navigate('/login', { replace: true });
return;
}
}, [role, navigate]);

if (role === 'FIRST_RESPONDER' || !role) {
return <Loader />;
} else {
return children;
}
}

ProtectedRoute.propTypes = ProtectedRouteProps;

/**
* Top-level application component. *
* @returns {React.ReactElement}
Expand Down Expand Up @@ -101,7 +129,11 @@ function App() {
<Route path="/patients/:patientId" element={<Patient />} />
<Route
path="/patients/register/:patientId"
element={<PatientRegistration />}
element={
<ProtectedRoute role={user?.role}>
<PatientRegistration />
</ProtectedRoute>
}
/>
<Route path="/admin/users" element={<AdminUsers />} />
<Route
Expand Down
2 changes: 0 additions & 2 deletions client/src/pages/patients/Patient.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export default function Patient() {
});
console.log(data, isSuccess, isError, isLoading);



useEffect(() => {
if (isError) {
navigate('/patients/register/' + patientId, {
Expand Down

0 comments on commit 5aa70dd

Please sign in to comment.