Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  export const checkAuth = (ChildComponent) => {
  2.  
  3.   class Authenticate extends React.Component {
  4.  
  5.     componentWillMount() {
  6.       if(!this.props.isAuthenticated) {
  7.         console.log("You need to be authenticated to access");
  8.         this.context.router.push('/login');
  9.       }
  10.     }
  11.  
  12.     componentWillUpdate() {
  13.       if(!this.props.isAuthenticated) {
  14.         console.log("You need to be authenticated to access");
  15.         this.context.router.push('/login');
  16.       }
  17.     }
  18.    
  19.     render() {
  20.       return (
  21.         <ChildComponent {...this.props} />
  22.       )
  23.     }
  24.   }
  25.  
  26.   Authenticate.propTypes = {
  27.     isAuthenticated: React.PropTypes.bool.isRequired
  28.   };
  29.  
  30.   Authenticate.contextTypes = {
  31.     router: React.PropTypes.object.isRequired
  32.   };
  33.  
  34.   function mapStateToProps(state) {
  35.     return {
  36.       isAuthenticated: state.auth.isAuthenticated
  37.     }
  38.   }
  39.  
  40.   return connect(mapStateToProps)(Authenticate);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement