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

Merge branch 'private' into 'master'

Implement mock Private route basing on user role

See merge request !24
parents 4aca6ca0 a1238c25
No related branches found
No related tags found
1 merge request!24Implement mock Private route basing on user role
Pipeline #11732 passed
...@@ -5,14 +5,23 @@ import { AuthUser } from 'components/AuthUser/AuthUser'; ...@@ -5,14 +5,23 @@ import { AuthUser } from 'components/AuthUser/AuthUser';
import { LandingPage } from 'components/LandingPage/LandingPage'; import { LandingPage } from 'components/LandingPage/LandingPage';
import { PrivateRoute } from 'components/api/PrivateRoute/PrivateRoute'; import { PrivateRoute } from 'components/api/PrivateRoute/PrivateRoute';
import { AuthRoutes, NonAuthRoutes } from 'components/api/routes'; import { AuthRoutes, NonAuthRoutes } from 'components/api/routes';
import { NotFound } from 'components/NotFound/NotFound';
import { Roles } from 'components/api/userRoles';
import { Unauthorized } from 'components/Unauthorized/Unauthorized';
export const App: FC = () => ( export const App: FC = () => (
<Router> <Router>
<div data-testid="App"> <div data-testid="App">
<Switch> <Switch>
<Route path={NonAuthRoutes.login} component={AuthUser} /> <Route path={NonAuthRoutes.signIn} component={AuthUser} />
<Route exact path={NonAuthRoutes.home} component={LandingPage} /> <Route exact path={NonAuthRoutes.home} component={LandingPage} />
<PrivateRoute path={AuthRoutes.dashboard} Component={HomePage} /> <PrivateRoute
path={AuthRoutes.dashboard}
Component={HomePage}
requiredRoles={[Roles.admin, Roles.operator, Roles.senior]}
/>
<Route path={NonAuthRoutes.unauthorized} component={Unauthorized} />
<Route component={NotFound} />
</Switch> </Switch>
</div> </div>
</Router> </Router>
......
...@@ -6,7 +6,7 @@ import { NonAuthRoutes } from 'components/api/routes'; ...@@ -6,7 +6,7 @@ import { NonAuthRoutes } from 'components/api/routes';
export const LandingPage: FC = () => ( export const LandingPage: FC = () => (
<> <>
<Button variant="contained"> <Button variant="contained">
<Link to={NonAuthRoutes.login}>Login</Link> <Link to={NonAuthRoutes.signIn}>Login</Link>
</Button> </Button>
<section> <section>
<h2>What is MoveAid?</h2> <h2>What is MoveAid?</h2>
......
import React, { FC } from 'react';
export const NotFound: FC = () => <h1>Page not found</h1>;
import React, { FC } from 'react';
export const Unauthorized: FC = () => <h1>You cannot access this page</h1>;
...@@ -9,23 +9,33 @@ import { NonAuthRoutes } from 'components/api/routes'; ...@@ -9,23 +9,33 @@ import { NonAuthRoutes } from 'components/api/routes';
type Props = { type Props = {
Component: React.FC<RouteProps>; Component: React.FC<RouteProps>;
path: string; path: string;
requiredRoles: string[];
}; };
/* eslint-disable react/jsx-props-no-spreading */ /* eslint-disable react/jsx-props-no-spreading */
export const PrivateRoute = ({ Component, path }: Props): JSX.Element => { export const PrivateRoute = ({
const isAuthed = false; Component,
const message = 'Please log in to view this page'; path,
requiredRoles,
}: Props): JSX.Element => {
const isAuthed = true;
const userHasRequiredRole = requiredRoles.includes('admin');
const message = userHasRequiredRole
? 'Please log in to view this page'
: 'Your role is not allowed';
return ( return (
<Route <Route
exact={false} exact={false}
path={path} path={path}
render={(props: RouteProps) => render={(props: RouteProps) =>
isAuthed ? ( isAuthed && userHasRequiredRole ? (
<Component {...props} /> <Component {...props} />
) : ( ) : (
<Redirect <Redirect
to={{ to={{
pathname: NonAuthRoutes.login, pathname: userHasRequiredRole
? NonAuthRoutes.signIn
: NonAuthRoutes.unauthorized,
state: { state: {
message, message,
requestedPath: path, requestedPath: path,
......
/**
* Allowed routes for authenticated users.
* Routes must be defined there and then reused across components
* importing AuthRoutes and NonAuthRoutes
*/
export enum AuthRoutes { export enum AuthRoutes {
dashboard = '/dashboard', dashboard = '/dashboard',
account = '/account', account = '/account',
} }
/**
* Allowed routes for non authenticated users
*/
export enum NonAuthRoutes { export enum NonAuthRoutes {
home = '/', home = '/',
login = '/login', signIn = '/signIn',
unauthorized = '/unauthorized', unauthorized = '/unauthorized',
} }
export enum Roles {
senior = 'senior',
admin = 'admin',
operator = 'operator',
}
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