zalando/zappr

View on GitHub
client/components/requireAuth.jsx

Summary

Maintainability
A
1 hr
Test Coverage
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'

/**
 * Based on https://github.com/joshgeller/react-redux-jwt-auth-example
 */
export default function requireAuth(WrappedComponent, redirect = '/login') {

  function mapStateToProps(state) {
    return {
      isAuthenticated: state.auth.isAuthenticated
    }
  }

  class RequireAuthComponent extends Component {
    static propTypes = {
      isAuthenticated: PropTypes.bool.isRequired
    };

    static contextTypes = {
      router: PropTypes.object.isRequired
    };

    componentWillMount() {
      this.checkAuth(this.props)
    }

    componentWillReceiveProps(props) {
      this.checkAuth(props)
    }

    checkAuth(props) {
      if (!props.isAuthenticated) {
        this.context.router.replace(redirect)
      }
    }

    render() {
      if (this.props.isAuthenticated) {
        return (<WrappedComponent {...this.props}/>)
      } else {
        return null
      }
    }
  }

  return connect(mapStateToProps)(RequireAuthComponent)
}