Skip to content

Instantly share code, notes, and snippets.

@Luke-SNAW
Last active October 28, 2021 04:32
Show Gist options
  • Save Luke-SNAW/e69057cddf2b36ae8b60daf740b6abc3 to your computer and use it in GitHub Desktop.
Save Luke-SNAW/e69057cddf2b36ae8b60daf740b6abc3 to your computer and use it in GitHub Desktop.
[Clean pattern for handling roles and permissions in large-scale React apps] #auth #API

https://itnext.io/clean-pattern-for-handling-roles-and-permissions-in-large-scale-react-apps-99531869ad71

  • ROLES - All the user roles in our application.
  • SCOPES - Scopes we pass to our gating wrapper.
  • PERMISSIONS - The map defines the set of scopes each user role possesses.
export const ROLES = {
  viewer: "VIEWER",
  editor: "EDITOR",
  owner: "OWNER"
};

export const SCOPES = {
  canCreate: "can-create",
  canEdit: "can-edit",
  canDelete: "can-delete",
  canView: "can-view"
};

export const PERMISSIONS = {
  [ROLES.viewer]: [SCOPES.canView],
  [ROLES.editor]: [SCOPES.canView, SCOPES.canEdit],
  [ROLES.owner]: [
    SCOPES.canView,
    SCOPES.canEdit,
    SCOPES.canCreate,
    SCOPES.canDelete
  ]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment