Ricardo Fearing
Aug 23, 2017 · 1 min read

This is awesome. I agree with Josh that it’s nice to have this without Redux but thought I’d share my implementation with Redux:

import React, { PropTypes, Component } from 'react';
import { connect } from 'react-redux';
/**
* HOC that Handles whether or not the user is allowed to see the page.
* @param {array} allowedRoles - user roles that are allowed to see the page.
* @returns {Component}
*/
export default function Authorization(allowedRoles) {
return WrappedComponent => {
class WithAuthorization extends Component {
static propTypes = {
user: PropTypes.object,
};
constructor(props) {
super(props);
}
render() {
const { user } = this.props;
if (allowedRoles.includes(user.account_type)) {
return <WrappedComponent {...this.props} />;
} else {
return <h1>No page for you!</h1>;
}
}
};
return connect(state => {
return { user: state.application.user };
})(WithAuthorization);
};
};

)

    Ricardo Fearing

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade