Skip to content

Instantly share code, notes, and snippets.

@lucasdu4rte
Created July 9, 2020 16:33
Show Gist options
  • Save lucasdu4rte/819bb0b4dde8641df7f730f724dcc885 to your computer and use it in GitHub Desktop.
Save lucasdu4rte/819bb0b4dde8641df7f730f724dcc885 to your computer and use it in GitHub Desktop.
import { useSelector } from 'react-redux'
function useAccessControl(mode, page) {
const accessControl = useSelector((state) => state.accessControl.data) || {}
const user = useSelector((state) => state.user.profile) || {}
function can(action) {
console.log(`accessControl.${mode}.${page}.${action}`)
return Boolean(accessControl[mode][page][action])
}
function is(role) {
const currentUserRole = user.hasAccess ? 'admin' : 'user'
return role === currentUserRole
}
return { can, is }
}
export default useAccessControl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment