import * as React from 'react'; import { Redirect, Route, RouteProps, RouteComponentProps } from "react-router-dom"; import { withSnackbar, WithSnackbarProps } from 'notistack'; import * as Authentication from './Authentication'; import { withAuthenticationContext, AuthenticationContextProps, AuthenticatedContext } from './AuthenticationContext'; type ChildComponent = React.ComponentType> | React.ComponentType; interface AuthenticatedRouteProps extends RouteProps, WithSnackbarProps, AuthenticationContextProps { component: ChildComponent; } type RenderComponent = (props: RouteComponentProps) => React.ReactNode; export class AuthenticatedRoute extends React.Component { render() { const { enqueueSnackbar, authenticationContext, component: Component, ...rest } = this.props; const { location } = this.props; const renderComponent: RenderComponent = (props) => { if (authenticationContext.me) { return ( ); } Authentication.storeLoginRedirect(location); enqueueSnackbar("Please log in to continue.", { variant: 'info' }); return ( ); } return ( ); } } export default withSnackbar(withAuthenticationContext(AuthenticatedRoute));