Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 32 additions & 73 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"react-error-overlay": "^6.0.9",
"react-google-recaptcha": "^2.1.0",
"react-iframe": "^1.8.0",
"react-router-dom": "^5.2.0",
"react-router-dom": "^6.30.1",
"react-scripts": "5.0.1",
"react-transition-group": "^4.4.2",
"universal-cookie": "^4.0.4"
Expand Down
49 changes: 10 additions & 39 deletions src/Components/Routing/PrivateRoute.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import { Navigate, useLocation } from 'react-router-dom';
import { membershipState } from '../../Enums';
import { allowedIf } from '../../Routes';
import { useUser } from '../../Components/context/UserContext';
import { useAuth } from '../../Components/context/AuthContext';

export default function PrivateRoute({
component: Component,
appProps,
...params
}) {
export default function PrivateRoute({ element: Component, appProps, ...params }) {
const { user } = useUser();
const { authenticated } = useAuth();
const location = useLocation();

// Check if the user's access level matches with route's access grant
const PERMISSION_LOOKUP_TABLE = {
[allowedIf.MEMBER]: user?.accessLevel >= membershipState.MEMBER,
[allowedIf.OFFICER_OR_ADMIN]: user?.accessLevel >= membershipState.OFFICER,
Expand All @@ -23,36 +19,11 @@ export default function PrivateRoute({

const isAllowed = PERMISSION_LOOKUP_TABLE[appProps.allowed] ?? false;

return (
<Route
{...params}
render={(props) => {
if (isAllowed) {
return <Component {...appProps} {...props} />;
} else if (authenticated) {
return (
<Redirect
to={{
pathname: '/',
}}
/>
);
} else {
return (
<Route
render={(props) => (
<Redirect
to={{
pathname:
'/login?redirect=' + encodeURIComponent(params.location.pathname),
state: { from: props.location },
}}
/>
)}
/>
);
}
}}
/>
);
if (isAllowed) {
return React.cloneElement(Component, { ...appProps });
} else if (authenticated) {
return <Navigate to="/" replace />;
} else {
return <Navigate to={`/login?redirect=${encodeURIComponent(location.pathname)}`} state={{ from: location }} replace />;
}
}
4 changes: 2 additions & 2 deletions src/Pages/Messaging/Messaging.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useState, useEffect } from 'react';
import { sendMessage, connectToRoom } from '../../APIFunctions/Messaging';
import { useParams, useHistory } from 'react-router-dom/cjs/react-router-dom.min';
import { useParams, useNavigate } from 'react-router-dom';
import { useUser } from '../../Components/context/UserContext';

export default function Messaging() {
const { id } = useParams();
const history = useHistory();
const history = useNavigate();
const { user } = useUser();
const [roomIdInput, setRoomIdInput] = useState(id || '');
const [roomIdSubmit, setRoomIdSubmit] = useState(id || 'general');
Expand Down
47 changes: 23 additions & 24 deletions src/Routing.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';

import PrivateRoute from './Components/Routing/PrivateRoute';
import NavBarWrapper from './Components/Navbar/NavBarWrapper';
Expand All @@ -19,7 +19,7 @@ export default function Routing({ appProps }) {

return (
<div>
<Switch>
<Routes>
{signedInRoutes.map(
({
path,
Expand All @@ -40,34 +40,33 @@ export default function Routing({ appProps }) {
/>);
}
return (
<PrivateRoute
<Route
key={index}
exact
path={path}
appProps={{
allowed: allowedIf,
redirect,
...appProps
}}
component={props => getCorrectComponent(props)}
element={
<PrivateRoute
appProps={{
allowed: allowedIf,
redirect,
...appProps
}}
>
{getCorrectComponent({})}
</PrivateRoute>
}
/>
);
}
)}
{signedOutRoutes.map(({ path, Component }, index) => {
return (
<Route
key={index}
exact
path={path}
render={props => (
<NavBarWrapper component={Component} {...props} {...appProps} />
)}
/>
);
})}
<Route component={NotFoundPage} />
</Switch>
{signedOutRoutes.map(({ path, Component }, index) => (
<Route
key={index}
path={path}
element={<NavBarWrapper component={Component} {...appProps} />}
/>
))}
<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
);
}
Loading