Skip to content

veedstudio/authzed-node

 
 

Repository files navigation

Authzed NodeJS Client

npm version License Build Status Mailing List Discord Server Twitter

This repository houses the NodeJS client library for Authzed.

Authzed is a database and service that stores, computes, and validates your application's permissions.

Developers create a schema that models their permissions requirements and use a client library, such as this one, to apply the schema to the database, insert data into the database, and query the data to efficiently check permissions in their applications.

Supported client API versions:

You can find more info on each API on the Authzed API reference documentation. Additionally, Protobuf API documentation can be found on the Buf Registry Authzed API repository.

See CONTRIBUTING.md for instructions on how to contribute and perform common tasks like building the project and running tests.

Getting Started

We highly recommend following the Protecting Your First App guide to learn the latest best practice to integrate an application with Authzed.

If you're interested in examples of a specific version of the API, they can be found in their respective folders in the examples directory.

Basic Usage

Installation

The project is packaged and distributed via NPM.

If you are using the typical npm toolchain, the command to install the library is:

npm i @authzed/authzed-node

Initializing a client

Everything required to connect and make API calls is located in a module respective to API version.

You will have to provide a your own API Token from the [Authzed dashboard] in place of t_your_token_here_1234567deadbeef in the following example:

const authzed = require('@authzed/authzed-node/v0');

const client = authzed.NewClient("t_your_token_here_1234567deadbeef");

Performing an API call

Because of the verbosity of these types, we recommend writing your own functions/methods to create these types from your existing application's models.

// Create the user Emilia
const emilia = new authzed.User();
const subjectRef = new authzed.ObjectAndRelation();
subjectReference.setNamespace("blog/user");
subjectReference.setObjectId("emilia");
subjectReference.setRelation("...");
emilia.setUserset(subjectRef);

// Create the permission "read the first post"
const readFirstPost = new authzed.ObjectAndRelation();
resourceAndPermission.setNamespace("blog/post");
resourceAndPermission.setObjectId("1");
resourceAndPermission.setRelation("read");

// Create a request object
const checkRequest = new authzed.CheckRequest();
request.setUser(emilia);
request.setTestUserset(readFirstPost);

// Is Emilia in the set of users that can read post #1?
client.check(checkRequest, function (err, response) {
    console.log(response);
    console.log(err);
});

Packages

No packages published

Languages

  • JavaScript 96.9%
  • Shell 3.1%