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 { 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 LoginRoute from 'components/helpers/Rerouters/LoginRoute';
......@@ -16,9 +16,9 @@ export default function App() {
<Router>
<Suspense fallback={false}>
<Switch>
<ProtectedRoute path="/tasks" component={<Tasks />} />
<ProtectedRoute path="/projects" component={<Projects />} />
<ProtectedRoute path="/stats" component={<Stats />} />
<ProtectedRoute path="/tasks" component={Tasks} />
<ProtectedRoute path="/projects" component={Projects} />
<ProtectedRoute path="/stats" component={Stats} />
<LoginRoute path="/login" component={Login} />
<LoginRoute path="/register" component={Register} />
<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 { useEffect } from 'react';
interface Props {
path: string,
exact?: boolean,
component: ComponentType<any>
}
export default function ProtectedRoute({ path, exact, component }: Props) {
if (!isLoggedIn()) {
return (
<Route path={path} exact={exact} component={component} />
);
}
export default function LoginRoute(props: RouteProps) {
const history = useHistory();
useEffect(() => {
if (isLoggedIn()) {
if (history.length === 0) {
history.push('/tasks');
} else {
history.goBack();;
}
}
})
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 Navigation from 'components/ui/Navigation';
import Header from 'components/ui/Header';
import { useEffect } from 'react';
interface Props {
path: string,
exact?: boolean,
component: JSX.Element
}
export default function ProtectedRoute({ path, exact, component }: Props) {
console.log(component);
if (isLoggedIn()) {
return (
<Route path={path} exact={exact} render={() =>
<>
<Header />
<Navigation />
{ component }
</>
} />
);
}
export default function ProtectedRoute(props: RouteProps) {
const history = useHistory();
useEffect(() => {
if (!isLoggedIn()) {
history.push('/login');
}
})
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