client/src/misc/AuthenticatedRoute.js
changeset 105 0a1d6560acac
child 129 d48946d164c6
equal deleted inserted replaced
104:d48a74232d22 105:0a1d6560acac
       
     1 // see https://gist.github.com/fdidron/ebcf52dc1ed62ff7d80725854d631a9e
       
     2 
       
     3 import PropTypes from 'prop-types';
       
     4 import React from 'react';
       
     5 import { Redirect, Route } from 'react-router';
       
     6 
       
     7 const AuthenticatedRoute = ({component, ...props}) => {
       
     8 
       
     9   const { store } = props;
       
    10   const state = store.getState();
       
    11   const isAuthenticated = state.isAuthenticated;
       
    12 
       
    13   if (isAuthenticated) {
       
    14     return <Route { ...props } component={ component } />;
       
    15   }
       
    16 
       
    17   return <Redirect to="/login" />;
       
    18 };
       
    19 
       
    20 AuthenticatedRoute.propTypes = {
       
    21   component: PropTypes.oneOfType([
       
    22     PropTypes.element,
       
    23     PropTypes.func
       
    24   ])
       
    25 };
       
    26 
       
    27 export default AuthenticatedRoute;