Private route component. Renders the child components if the user is logged in
(and in an authorized role for the route). If the user isn't logged in they are
redirected to the landing page (login/signup). If the user is logged in but doesn't have
the correct role, they are redirected to the home page. Used inside Switch component that is imported from 'react-router-dom'.
Parameters:
Name |
Type |
Description |
props |
Object
|
Properties
Name |
Type |
Attributes |
Description |
loggedIn |
boolean
|
undefined
|
|
User's loggedIn status. Can be undefined if user isn't loggedIn. |
path |
string
|
|
Route's path |
children |
node
|
|
Components that the route renders. |
role |
string
|
<optional>
|
Required if private route is role specific. User's current role. |
roles |
"worker"
|
"business"
|
"agency"
|
<optional>
|
Required if private route is role specific.
Roles that are authorized. |
|
- Source:
Example
<Switch>
<PrivateRoute path="/profile" loggedIn={ loggedIn }>
<ProfilePage />
</PrivateRoute>
<PrivateRoute
path="/workers"
role={ data ? data.role : undefined }
roles={ [Role.Business, Role.Agency] }
loggedIn={ loggedIn }>
<WorkersPage />
</PrivateRoute>
</Switch>