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.

42 lines
1.5 KiB

  1. import * as React from 'react';
  2. import { Redirect, Route, RouteProps, RouteComponentProps } from "react-router-dom";
  3. import { withSnackbar, WithSnackbarProps } from 'notistack';
  4. import * as Authentication from './Authentication';
  5. import { withAuthenticationContext, AuthenticationContextProps, AuthenticatedContext } from './AuthenticationContext';
  6. type ChildComponent = React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
  7. interface AuthenticatedRouteProps extends RouteProps, WithSnackbarProps, AuthenticationContextProps {
  8. component: ChildComponent;
  9. }
  10. type RenderComponent = (props: RouteComponentProps<any>) => React.ReactNode;
  11. export class AuthenticatedRoute extends React.Component<AuthenticatedRouteProps> {
  12. render() {
  13. const { enqueueSnackbar, authenticationContext, component: Component, ...rest } = this.props;
  14. const { location } = this.props;
  15. const renderComponent: RenderComponent = (props) => {
  16. if (authenticationContext.me) {
  17. return (
  18. <AuthenticatedContext.Provider value={authenticationContext as AuthenticatedContext}>
  19. <Component {...props} />
  20. </AuthenticatedContext.Provider>
  21. );
  22. }
  23. Authentication.storeLoginRedirect(location);
  24. enqueueSnackbar("Please log in to continue.", { variant: 'info' });
  25. return (
  26. <Redirect to='/' />
  27. );
  28. }
  29. return (
  30. <Route {...rest} render={renderComponent} />
  31. );
  32. }
  33. }
  34. export default withSnackbar(withAuthenticationContext(AuthenticatedRoute));