-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web: Refactored routes to improve compatibility with react-router v6 (#…
…330) * Refactored routes to improve compatibility with react-router v6 * Fixed ProtectedRoute
- Loading branch information
Showing
25 changed files
with
246 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,60 @@ | ||
import React, { useContext } from "react"; | ||
import { Route } from "react-router-dom"; | ||
|
||
import { Alert } from "./ErrorBoundary"; | ||
import { useLocation } from "react-router"; | ||
import { Redirect, Route } from "react-router-dom"; | ||
|
||
import { AuthContext } from "../auth"; | ||
|
||
export default function ProtectedRoute(props) { | ||
export function ConditionalRoute({ children, condition, fallback, ...props }) { | ||
return <Route {...props}>{condition ? children : fallback}</Route>; | ||
} | ||
|
||
export function ProtectedRoute({ children, condition, ...props }) { | ||
const auth = useContext(AuthContext); | ||
const location = useLocation(); | ||
return ( | ||
<ConditionalRoute | ||
condition={auth.isAuthenticated} | ||
fallback={ | ||
<Redirect | ||
to={{ | ||
pathname: "/login", | ||
state: { | ||
prevLocation: location, | ||
error: | ||
"You need to authenticate before accessing this page", | ||
}, | ||
}} | ||
/> | ||
} | ||
{...props} | ||
> | ||
{condition || condition === undefined ? ( | ||
children | ||
) : ( | ||
<Redirect | ||
to={{ | ||
pathname: "/", | ||
state: { | ||
error: `You don't have permission to access '${location.pathname}'`, | ||
}, | ||
}} | ||
/> | ||
)} | ||
</ConditionalRoute> | ||
); | ||
} | ||
|
||
export function AdministrativeRoute(args) { | ||
const auth = useContext(AuthContext); | ||
return <ProtectedRoute condition={auth.isAdmin} {...args} />; | ||
} | ||
|
||
export function AttributeRoute(args) { | ||
const auth = useContext(AuthContext); | ||
function routeRender(renderProps) { | ||
if (!auth.isAuthenticated) { | ||
auth.logout("You need to authenticate before accessing this page"); | ||
return []; | ||
} | ||
return props.condition ? ( | ||
<props.component {...renderProps} /> | ||
) : ( | ||
<div className="container-fluid"> | ||
<Alert error="You don't have permission to see that page" /> | ||
</div> | ||
); | ||
} | ||
return <Route {...props} component={undefined} render={routeRender} />; | ||
return ( | ||
<ProtectedRoute | ||
condition={auth.hasCapability("managing_attributes")} | ||
{...args} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.