-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathprotected-route.jsx
More file actions
32 lines (29 loc) · 955 Bytes
/
protected-route.jsx
File metadata and controls
32 lines (29 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import React, { useEffect } from 'react';
import { Route, Redirect } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import { getIsUserOwner } from './modules/IDE/selectors/users';
import { resetProject } from './modules/IDE/actions/project';
// eslint-disable-next-line react/prop-types
const ProtectedSketchRoute = ({ component: Component, ...rest }) => {
const project = useSelector((state) => state.project);
const isUserOwner = useSelector(getIsUserOwner);
const dispatch = useDispatch();
const hasAccess = isUserOwner || project.visibility !== 'Private';
useEffect(() => {
if (!hasAccess) {
dispatch(resetProject());
}
}, [hasAccess, dispatch]);
return (
<Route
{...rest}
render={(props) => {
if (!hasAccess) {
return <Redirect to="/" />;
}
return <Component {...props} />;
}}
/>
);
};
export default ProtectedSketchRoute;