|
@@ -6,33 +6,46 @@ import token from '../../../utils/token'
|
|
|
import * as routes from './../../../constants/routes';
|
|
|
import PermissionDenied from './../../public-components/permissionDenied';
|
|
|
|
|
|
-export default ({ component: Component, user, access, ...rest }) => (
|
|
|
+export default ({ component: Component, user: { data }, tokenAuth, access, ...rest }) => (
|
|
|
<Route
|
|
|
{...rest}
|
|
|
render={props => {
|
|
|
- const storagedUser = JSON.parse(localStorage.getItem(token));
|
|
|
- const data = user.data || storagedUser || null;
|
|
|
+ let checkedData;
|
|
|
+
|
|
|
+ if (data) {
|
|
|
+ checkedData = data;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ const storagedUser = JSON.parse(localStorage.getItem(token));
|
|
|
+ if (storagedUser) {
|
|
|
+ checkedData = storagedUser;
|
|
|
+ tokenAuth(storagedUser);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ checkedData = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
if (access === 'public') {
|
|
|
return <Component {...props} />
|
|
|
}
|
|
|
-
|
|
|
- if ( (access === 'user-only' || access === 'admin-only') && !data ) {
|
|
|
- return <Redirect to={routes.SIGN_IN}/>
|
|
|
+
|
|
|
+ if ((access === 'user-only' || access === 'admin-only') && !checkedData) {
|
|
|
+ return <Redirect to={routes.SIGN_IN} />
|
|
|
}
|
|
|
-
|
|
|
- if (access === 'user-only' && data) {
|
|
|
+
|
|
|
+ if (access === 'user-only' && checkedData) {
|
|
|
return <Component {...props} />
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
if (access === 'admin-only') {
|
|
|
- if (data.is_admin) {
|
|
|
+ if (checkedData.is_admin) {
|
|
|
return <Component {...props} />
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
return <PermissionDenied />
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}}
|
|
|
/>
|
|
|
)
|