Skip to content

Protected route created #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 2 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BrowserRouter, Route, Switch } from "react-router-dom";
import theme from "./theme";
import GlobalStyles from "./GlobalStyles";
import Pace from "./shared/components/Pace";
import ProtectedRoute from "./shared/components/ProtectedRoute";

const LoggedInComponent = lazy(() => import("./logged_in/components/Main"));

Expand All @@ -19,9 +20,7 @@ function App() {
<Pace color={theme.palette.primary.light} />
<Suspense fallback={<Fragment />}>
<Switch>
<Route path="/c">
<LoggedInComponent />
</Route>
<ProtectedRoute path="/c" component={LoggedInComponent} />
<Route>
<LoggedOutComponent />
</Route>
Expand Down
1 change: 1 addition & 0 deletions src/logged_in/components/navigation/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ function NavBar(props) {
divider={index !== menuItems.length - 1}
className={classes.permanentDrawerListItem}
onClick={() => {
localStorage.removeItem('accessToken');
links.current[index].click();
}}
aria-label={
Expand Down
2 changes: 2 additions & 0 deletions src/logged_out/components/register_login/LoginDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function LoginDialog(props) {
}, 1500);
} else {
setTimeout(() => {
const accessToken = btoa(loginEmail.current.value);
localStorage.setItem('accessToken', accessToken);
history.push("/c/dashboard");
}, 150);
}
Expand Down
22 changes: 22 additions & 0 deletions src/shared/components/ProtectedRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import { Route, Redirect } from 'react-router-dom';

const isAuthenticated = () => {
const accessToken = localStorage.getItem('accessToken');
return accessToken === btoa("[email protected]");
};

const ProtectedRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
isAuthenticated() ? (
<Component {...props} />
) : (
<Redirect to="/login" />
)
}
/>
);

export default ProtectedRoute;