Skip to content
Snippets Groups Projects
Commit 156d4286 authored by Bernard Roland (Student Com20)'s avatar Bernard Roland (Student Com20)
Browse files

Updated ProtectedRoute and LoginRoute

parent 9561ee3d
No related branches found
No related tags found
No related merge requests found
import { Suspense, lazy } from 'react'; import { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; import { BrowserRouter as Router, Switch, Route, } from 'react-router-dom';
import ProtectedRoute from 'components/helpers/Rerouters/ProtectedRoute'; import ProtectedRoute from 'components/helpers/Rerouters/ProtectedRoute';
import LoginRoute from 'components/helpers/Rerouters/LoginRoute'; import LoginRoute from 'components/helpers/Rerouters/LoginRoute';
...@@ -16,9 +16,9 @@ export default function App() { ...@@ -16,9 +16,9 @@ export default function App() {
<Router> <Router>
<Suspense fallback={false}> <Suspense fallback={false}>
<Switch> <Switch>
<ProtectedRoute path="/tasks" component={<Tasks />} /> <ProtectedRoute path="/tasks" component={Tasks} />
<ProtectedRoute path="/projects" component={<Projects />} /> <ProtectedRoute path="/projects" component={Projects} />
<ProtectedRoute path="/stats" component={<Stats />} /> <ProtectedRoute path="/stats" component={Stats} />
<LoginRoute path="/login" component={Login} /> <LoginRoute path="/login" component={Login} />
<LoginRoute path="/register" component={Register} /> <LoginRoute path="/register" component={Register} />
<Route path="/" component={Home} /> <Route path="/" component={Home} />
......
export const isLoggedIn = (): boolean => true;
export function isLoggedIn() {
return true;
}
import { ComponentType } from 'react';
import { Redirect, Route } from 'react-router-dom'; import { Route, RouteProps, useHistory } from 'react-router-dom';
import { isLoggedIn } from 'adapters/api'; import { isLoggedIn } from 'adapters/api';
import { useEffect } from 'react';
interface Props { export default function LoginRoute(props: RouteProps) {
path: string, const history = useHistory();
exact?: boolean, useEffect(() => {
component: ComponentType<any> if (isLoggedIn()) {
} if (history.length === 0) {
export default function ProtectedRoute({ path, exact, component }: Props) { history.push('/tasks');
if (!isLoggedIn()) { } else {
return ( history.goBack();;
<Route path={path} exact={exact} component={component} /> }
); }
} })
return ( return (
<Redirect to="/tasks"/> <Route {...props} />
); );
} }
\ No newline at end of file
import { Redirect, Route } from 'react-router-dom'; import { Route, RouteProps, useHistory } from 'react-router-dom';
import { isLoggedIn } from 'adapters/api'; import { isLoggedIn } from 'adapters/api';
import Navigation from 'components/ui/Navigation'; import { useEffect } from 'react';
import Header from 'components/ui/Header';
interface Props { export default function ProtectedRoute(props: RouteProps) {
path: string, const history = useHistory();
exact?: boolean, useEffect(() => {
component: JSX.Element if (!isLoggedIn()) {
} history.push('/login');
}
})
export default function ProtectedRoute({ path, exact, component }: Props) {
console.log(component);
if (isLoggedIn()) {
return (
<Route path={path} exact={exact} render={() =>
<>
<Header />
<Navigation />
{ component }
</>
} />
);
}
return ( return (
<Redirect to="/login" /> <Route {...props} />
); );
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment