2019-12-07 16:58:52 +05:30
|
|
|
import React, { ComponentType } from 'react';
|
|
|
|
import { Route, Redirect, RouteProps } from 'react-router-dom';
|
|
|
|
import { Location } from 'history';
|
2020-07-22 15:31:12 +05:30
|
|
|
|
|
|
|
import { connect } from 'app/functions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { getActiveAccount } from 'app/components/accounts/reducer';
|
|
|
|
import { Account } from 'app/components/accounts';
|
2019-12-07 16:58:52 +05:30
|
|
|
|
|
|
|
interface Props extends RouteProps {
|
2020-05-24 04:38:24 +05:30
|
|
|
component: ComponentType<any>;
|
|
|
|
account: Account | null;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
const PrivateRoute = ({ account, component: Component, ...rest }: Props) => (
|
2020-05-24 04:38:24 +05:30
|
|
|
<Route
|
|
|
|
{...rest}
|
|
|
|
render={(props: { location: Location }) =>
|
|
|
|
!account || !account.token ? <Redirect to="/login" /> : <Component {...props} />
|
|
|
|
}
|
|
|
|
/>
|
2019-12-07 16:58:52 +05:30
|
|
|
);
|
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
export default connect((state) => ({
|
2020-05-24 04:38:24 +05:30
|
|
|
account: getActiveAccount(state),
|
2019-12-07 16:58:52 +05:30
|
|
|
}))(PrivateRoute);
|