daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Feb 17th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top