import React from 'react'
import { Redirect, Route } from 'react-router-dom'
import store from '@/store'

function PrivateRoute ({
  component: Component,
  redirectPath = '/login',
  ...rest
}) {
  return (
    <Route
      {...rest}
      render={props =>
        store.userId && store.token ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: redirectPath,
              state: { from: props.location }
            }}
          />
        )
      }
    />
  )
}

export default PrivateRoute
