Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.2 KiB

  1. import * as React from 'react';
  2. import { Redirect, Route, RouteProps, RouteComponentProps } from "react-router-dom";
  3. import { withAuthenticationContext, AuthenticationContextProps } from './AuthenticationContext';
  4. import * as Authentication from './Authentication';
  5. import { WithFeaturesProps, withFeatures } from '../features/FeaturesContext';
  6. interface UnauthenticatedRouteProps extends RouteProps, AuthenticationContextProps, WithFeaturesProps {
  7. component: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
  8. }
  9. type RenderComponent = (props: RouteComponentProps<any>) => React.ReactNode;
  10. class UnauthenticatedRoute extends Route<UnauthenticatedRouteProps> {
  11. public render() {
  12. const { authenticationContext, component: Component, features, ...rest } = this.props;
  13. const renderComponent: RenderComponent = (props) => {
  14. if (authenticationContext.me) {
  15. return (<Redirect to={Authentication.fetchLoginRedirect(features)} />);
  16. }
  17. return (<Component {...props} />);
  18. }
  19. return (
  20. <Route {...rest} render={renderComponent} />
  21. );
  22. }
  23. }
  24. export default withFeatures(withAuthenticationContext(UnauthenticatedRoute));