client/src/misc/AuthenticatedRoute.js
author ymh <ymh.work@gmail.com>
Tue, 18 Dec 2018 02:29:05 +0100
changeset 201 942abb06295d
parent 168 ea92f4fe783d
permissions -rw-r--r--
Added tag 0.2.1 for changeset 1b9b9401ba7c

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

import PropTypes from 'prop-types';
import React from 'react';
import { Redirect, Route } from 'react-router';
import { isAuthenticated } from '../selectors/authSelectors';

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

  const { store } = props;
  const state = store.getState();

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

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

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

export default AuthenticatedRoute;