Skip to content

Instantly share code, notes, and snippets.

@babie
Created July 30, 2017 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babie/e35adea2425662814eb915be735e327f to your computer and use it in GitHub Desktop.
Save babie/e35adea2425662814eb915be735e327f to your computer and use it in GitHub Desktop.
Authorization and default currentUser prop by HOC
import React, { Component } from 'react'
import { connect } from 'react-redux'
const Authorization = (allowRoles) => {
return (WrappedComponent) => {
class WithAuthorization extends Component {
render () {
const { role } = this.props.currentUser
if (allowRoles.includes(role)) {
return <WrappedComponent {...this.props} />
} else {
return <h1>No page for you...</h1>
}
}
}
const mapStateToProps = (state, ownProps) => {
return {
currentUser: state.sharesReducer.usersReducer.currentUser
}
}
const mapDispatchToProps = (dispatch) => {
return {}
}
return connect(mapStateToProps, mapDispatchToProps)(WithAuthorization)
}
}
const Seller = Authorization(['admin', 'sellers'])
const Buyer = Authorization(['admin', 'buyers'])
const Admin = Authorization(['admin'])
export default {
Seller,
Buyer,
Admin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment