26 lines
828 B
TypeScript
Raw Normal View History

2019-12-07 13:28:52 +02:00
import React, { ComponentType } from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom';
import { Location } from 'history';
import { connect } from 'react-redux';
import { getActiveAccount } from 'app/components/accounts/reducer';
import { Account } from 'app/components/accounts';
import { RootState } from 'app/reducers';
2019-12-07 13:28:52 +02:00
interface Props extends RouteProps {
2020-05-24 02:08:24 +03:00
component: ComponentType<any>;
account: Account | null;
2019-12-07 13:28:52 +02:00
}
const PrivateRoute = ({ account, component: Component, ...rest }: Props) => (
2020-05-24 02:08:24 +03:00
<Route
{...rest}
render={(props: { location: Location }) =>
!account || !account.token ? <Redirect to="/login" /> : <Component {...props} />
}
/>
2019-12-07 13:28:52 +02:00
);
export default connect((state: RootState) => ({
2020-05-24 02:08:24 +03:00
account: getActiveAccount(state),
2019-12-07 13:28:52 +02:00
}))(PrivateRoute);