Getting Started

access-gate makes it easy to define, manage, and evaluate role-based access control (RBAC) in your applications. Follow these steps to get started:

Installation

Install the library via npm or yarn:

npm install access-gate
# or
yarn add access-gate

Basic Usage

To start using access-gate, you need to create a Gate instance, define policies, and evaluate decisions.

import { Gate, Policy } from "access-gate";

// Create a new Gate instance
const gate = new Gate();

// Define a policy for managing posts
const postPolicy = new Policy("post");
postPolicy.define("update", (user, post) => user.id === post.authorId);

// Add the policy to the Gate
gate.addPolicy(postPolicy);

// Create a representative for the current user
const representative = gate.build({ id: 1 });

// Check if the user can update the post
const decision = representative.access("post", "update").can({ authorId: 1 });
console.log(decision); // true

Next Steps

  • Learn about Core Concepts such as Policies, Guards, Decisions, and Representatives.
  • Explore the API Reference to dive deeper into the library’s functionality.
  • Check out Examples for real-world usage scenarios.