Skip to content
Snippets Groups Projects
Commit 4aca6ca0 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Merge branch 'fix-test' into 'master'

Private route

See merge request !23
parents 32c32392 8e0b31ab
No related branches found
No related tags found
1 merge request!23Private route
Pipeline #11729 passed
......@@ -3,14 +3,16 @@ import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import { HomePage } from 'components/HomePage/HomePage';
import { AuthUser } from 'components/AuthUser/AuthUser';
import { LandingPage } from 'components/LandingPage/LandingPage';
import { PrivateRoute } from 'components/api/PrivateRoute/PrivateRoute';
import { AuthRoutes, NonAuthRoutes } from 'components/api/routes';
export const App: FC = () => (
<Router>
<div data-testid="App">
<Switch>
<Route path="/auth" component={AuthUser} />
<Route exact path="/" component={LandingPage} />
<Route exact path="/home" component={HomePage} />
<Route path={NonAuthRoutes.login} component={AuthUser} />
<Route exact path={NonAuthRoutes.home} component={LandingPage} />
<PrivateRoute path={AuthRoutes.dashboard} Component={HomePage} />
</Switch>
</div>
</Router>
......
import React, { FC } from 'react';
import Button from '@material-ui/core/Button';
import { Link } from 'react-router-dom';
import { NonAuthRoutes } from 'components/api/routes';
export const LandingPage: FC = () => (
<>
<Button variant="contained">
<Link to="/auth">Login</Link>
<Link to={NonAuthRoutes.login}>Login</Link>
</Button>
<section>
<h2>What is MoveAid?</h2>
......
import React from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom';
import { NonAuthRoutes } from 'components/api/routes';
/**
* A wrapper for <Route> that redirects to the login screen if you're not yet authenticated.
* */
type Props = {
Component: React.FC<RouteProps>;
path: string;
};
/* eslint-disable react/jsx-props-no-spreading */
export const PrivateRoute = ({ Component, path }: Props): JSX.Element => {
const isAuthed = false;
const message = 'Please log in to view this page';
return (
<Route
exact={false}
path={path}
render={(props: RouteProps) =>
isAuthed ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: NonAuthRoutes.login,
state: {
message,
requestedPath: path,
},
}}
/>
)
}
/>
);
};
export enum AuthRoutes {
dashboard = '/dashboard',
account = '/account',
}
export enum NonAuthRoutes {
home = '/',
login = '/login',
unauthorized = '/unauthorized',
}
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