Implement public/private routing

This commit is contained in:
2020-08-19 20:04:52 +02:00
parent 125a03f529
commit ec65c3b9dd
13 changed files with 110 additions and 82 deletions

View File

@@ -2,10 +2,9 @@ import React from 'react';
import { Helmet } from 'react-helmet';
export default function TitleComponent({ title }) {
var defaultTitle = 'Meeting Planner';
return (
<Helmet>
<title>{title ? `${title} | Meeting Planner` : defaultTitle}</title>
<title>{title ? `${title} | Meeting Planner` : 'Meeting Planner'}</title>
</Helmet>
);
}

View File

@@ -16,13 +16,11 @@ export default function NavBar({ title }) {
alignItems: 'center',
}}
>
{/* This hidden nav is a hack to have the title perfectly centered. */}
<Nav style={{ visibility: 'hidden' }}>
{/* This hidden nav is a hack to have the title perfectly centered. */}
<MenuDropdown />
</Nav>
<div>
<h3>{title}</h3>
</div>
<MenuDropdown />
</Navbar.Body>
</Navbar>

View File

@@ -0,0 +1,29 @@
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import { getToken } from '../../utils/common';
const isLoggedIn = getToken();
const PrivateRoute = ({ component: Component, user, ...rest }) => {
return (
<Route
{...rest}
render={(props) =>
isLoggedIn ? (
<Component {...rest} {...props} />
) : (
<Redirect
to={{
pathname: '/login',
state: {
from: props.location,
},
}}
/>
)
}
/>
);
};
export default PrivateRoute;

View File

@@ -0,0 +1,22 @@
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import { getToken } from '../../utils/common';
const isLoggedIn = getToken();
function PublicRoute({ component: Component, ...rest }) {
return (
<Route
{...rest}
render={(props) =>
!isLoggedIn ? (
<Component {...props} />
) : (
<Redirect to={{ pathname: '/dashboard' }} />
)
}
/>
);
}
export default PublicRoute;