client/src/misc/AuthenticatedRoute.js
author Riwad Salim
Wed, 15 Aug 2018 23:39:02 +0200
changeset 145 5d2bc8c877ea
parent 129 d48946d164c6
child 168 ea92f4fe783d
permissions -rw-r--r--
Adding class for specific css in App, Navbar and SessionList components

// see https://gist.github.com/fdidron/ebcf52dc1ed62ff7d80725854d631a9e

import PropTypes from 'prop-types';
import React from 'react';
import { Redirect, Route } from 'react-router';

const AuthenticatedRoute = ({component, ...props}) => {

  const { store } = props;
  const state = store.getState();
  const isAuthenticated = state.getIn(['authStatus', 'isAuthenticated']);

  if (isAuthenticated) {
    return <Route { ...props } component={ component } />;
  }

  return <Redirect to="/login" />;
};

AuthenticatedRoute.propTypes = {
  component: PropTypes.oneOfType([
    PropTypes.element,
    PropTypes.func
  ])
};

export default AuthenticatedRoute;