Kaynağa Gözat

auto-auth is done -> logic is moved into router, algo optimised

Maxim 5 yıl önce
ebeveyn
işleme
ec1882a312
2 değiştirilmiş dosya ile 27 ekleme ve 21 silme
  1. 25 12
      src/components/common/protectedRoute/index.js
  2. 2 9
      src/router.js

+ 25 - 12
src/components/common/protectedRoute/index.js

@@ -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 />
             }
-            
+
         }}
     />
 )

+ 2 - 9
src/router.js

@@ -16,16 +16,8 @@ import fakeToken from './utils/token'
 import { bindActionCreators } from "redux";
 
 class Router extends React.Component {
-
-    componentDidMount() {
-        const storagedUser = localStorage.getItem(fakeToken);
-        const { tokenAuth } = this.props;
-
-        storagedUser && tokenAuth(JSON.parse(storagedUser));
-    }
-
     render() {
-        const { user } = this.props;
+        const { user, tokenAuth } = this.props;
 
         return (
             <div className="app">
@@ -39,6 +31,7 @@ class Router extends React.Component {
                                 access={route.access}
                                 user={user}
                                 key={route}
+                                tokenAuth={tokenAuth}
                                 exact
                             />
                         )}