Guest User

Untitled

a guest
Feb 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import * as React from 'react';
  2. import { withRouter } from 'react-router-dom';
  3.  
  4. import { isLoggedIn } from '../../security/authentication';
  5. import routeTo from '../utils/routeTo';
  6.  
  7. const authenticatedMiddleware = Component => {
  8. class AuthenticatedMiddleware extends React.PureComponent {
  9. componentDidMount() {
  10. this.redirectIfNotAuthenticated();
  11. }
  12.  
  13. componentDidUpdate() {
  14. this.redirectIfNotAuthenticated();
  15. }
  16.  
  17. redirectIfNotAuthenticated = async () => {
  18. if (!isLoggedIn()) {
  19. logout();
  20. this.props.history.push(routeTo('auth.login'));
  21. }
  22. };
  23.  
  24. render() {
  25. if (!isLoggedIn()) {
  26. return null;
  27. }
  28.  
  29. return <Component {...this.props} />;
  30. }
  31. }
  32.  
  33. return AuthenticatedMiddleware;
  34. };
  35.  
  36. export default authenticatedMiddleware;
Add Comment
Please, Sign In to add comment