@@ -1,5 +1,6 @@
|
||||
import React from "react";
|
||||
import { Redirect, Route } from "react-router-dom";
|
||||
import { hasRole } from "../../utils/roles";
|
||||
|
||||
export const TYPES = {
|
||||
PUBLIC: 0,
|
||||
@@ -7,11 +8,19 @@ export const TYPES = {
|
||||
PROTECTED: 2,
|
||||
};
|
||||
|
||||
export const AuthRoute = ({ token, type, ...others }) => {
|
||||
if (!token && type === TYPES.PROTECTED) {
|
||||
export const AuthRoute = ({ token, type, roles, groups, ...others }) => {
|
||||
if (type === TYPES.PROTECTED && !token) {
|
||||
return <Redirect to="/" />;
|
||||
} else if (token && type === TYPES.GUEST) {
|
||||
} else if (type === TYPES.GUEST && token) {
|
||||
return <Redirect to="/home" />;
|
||||
} else if (
|
||||
type === TYPES.PROTECTED &&
|
||||
token &&
|
||||
roles &&
|
||||
!hasRole(roles, groups)
|
||||
) {
|
||||
return <Redirect to="/home" />;
|
||||
}
|
||||
|
||||
return <Route render {...others} />;
|
||||
};
|
||||
|
||||
@@ -4,14 +4,16 @@ import { Switch } from "react-router-dom";
|
||||
import { AuthRoute, TYPES } from "../Routes/AuthRoute";
|
||||
import { MessageBar } from "../MessageBar";
|
||||
import { useUserContext } from "../Contexts/UserContext";
|
||||
import { Roles } from "../../utils/roles";
|
||||
|
||||
const SSOForm = React.lazy(() => import("../SSOForm"));
|
||||
const Home = React.lazy(() => import("../Home"));
|
||||
const FileUploadForm = React.lazy(() => import("../FileUploadForm"));
|
||||
const VehicleAddForm = React.lazy(() => import("../VehicleAddForm"));
|
||||
const PageNotFound = React.lazy(() => import("../404"));
|
||||
|
||||
const SiteRoutes = () => {
|
||||
const { token } = useUserContext();
|
||||
const { token, groups } = useUserContext();
|
||||
return (
|
||||
<Suspense fallback={"Loading..."}>
|
||||
<MessageBar />
|
||||
@@ -25,15 +27,25 @@ const SiteRoutes = () => {
|
||||
/>
|
||||
<AuthRoute
|
||||
path="/home"
|
||||
render={() => <Home />}
|
||||
type={TYPES.PROTECTED}
|
||||
token={token}
|
||||
/>
|
||||
<AuthRoute
|
||||
path="/package-upload"
|
||||
render={() => <FileUploadForm />}
|
||||
type={TYPES.PROTECTED}
|
||||
token={token}
|
||||
groups={groups}
|
||||
roles={[Roles.CREATE]}
|
||||
/>
|
||||
<AuthRoute
|
||||
path="/vehicle-add"
|
||||
render={() => <VehicleAddForm />}
|
||||
type={TYPES.PROTECTED}
|
||||
token={token}
|
||||
groups={groups}
|
||||
roles={[Roles.CREATE]}
|
||||
/>
|
||||
<PageNotFound />
|
||||
</Switch>
|
||||
|
||||
Reference in New Issue
Block a user