client/src/misc/AuthenticatedRoute.js
author Alexandre Segura <mex.zktk@gmail.com>
Thu, 29 Jun 2017 12:05:09 +0200
changeset 105 0a1d6560acac
child 129 d48946d164c6
permissions -rw-r--r--
Introduce authenticated routes.

// 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.isAuthenticated;

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

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

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

export default AuthenticatedRoute;