2.0.1 • Published 2 years ago

@permify/node-permify v2.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

Permify Node.js Client

npm Twitter URL

Use Permify in server-side Node.js Projects.

Install

npm install @permify/node-permify

Usage

To get started, create Permify client using your Workspace id and API private key:

with require

const Permify = require("@permify/node-permify");

const permifyClient = new Permify.Client("workspace_id", "private_token");

with import

import { Client as PermifyClient } from "@permify/node-permify";

const permifyClient = new PermifyClient("workspace_id", "private_token");

Create Group

In order to start using Permify you need to have at least one group entity which is part of your workspace. This method creates a group entity in Permify.

const payload = {
    //If your app is a multi-tenant, id represents your group or tenant's id.
    //If your app is not multi-tenant or haven't got group entity,
    //Give any kind of identier value in here to create one.
    id: 'test_cs1234utg',
    
    //Name of the group
    name: 'test_name'
}

permifyClient.createGroup(payload)
    .then((group) => {
        console.log(group)
    })
    .catch((error) => {
        console.log(error.response)
    });

Create User

This method creates a user entity in Permify. You need a group, which the created user must belong.

const payload = {
    //Id of the user to be created in your app
    id: "id",

    //id of group
    group_id: "group id",

    // name of the user
    name: "name",

    // photo url of the user
    photo: "",

    // role names array
    role_names: [],

    // custom attributes
    attributes: {}
};

permifyClient.createUser(payload).then((user) => {
        console.log(user)
    })
    .catch((error) => {
        console.log(error.response)
    });

Create Role

This method creates a role entity in Permify.

permifyClient.createRole('group_id', {name: 'test_role'})
    .then((role) => {
        console.log(role)
    })
    .catch((error) => {
        console.log(error.response)
    });

Create Rule

This method creates a rule entity in Permify.

Sample Rules

Is the user senior?

user.attributes.tenure > 8

Is the user manager?

"manager" in user.roles

Is the user admin?

"admin" in user.roles

Is the user the owner of the resource?

user.id == resource.attributes.owner_id
const payload = {
    // name of rule
    name: "name",

    // conditions
    conditions: []
};

permifyClient.createRule(payload).then((rule) => {
        console.log(rule)
    })
    .catch((error) => {
        console.log(error.response.data)
    });

Create Option

This method creates a option entity in Permify.

const payload = {
    // name of option
    name: "name",

    // rule names
    rule_names: []
};

permifyClient.createOption(payload).then((option) => {
        console.log(option)
    })
    .catch((error) => {
        console.log(error.response.data)
    });

Create Policy

This method creates a policy entity in Permify.

const payload = {
    // name of policy
    name: "name",

    // rule names
    option_names: []
};

permifyClient.createPolicy(payload).then((policy) => {
        console.log(policy)
    })
    .catch((error) => {
        console.log(error.response.data)
    });

IsAuthorized

This method returns a decision about whether the user is authorized for this action with the given parameters.

Parameters

  • PolicyName (mandatory)

Custom Permify Policy name.

  • UserID (optional)

Id of the User

  • ResourceID (optional)

Id of the Resource, mandatory if any resource used or accessed when creating Rule/Rules.

  • ResourceType (optional)

Type or name of the Resource, mandatory if any resource used or accessed when creating Rule/Rules.

const payload = {
    // name of policy
    policy_name: "policy name",
    
    // id of user
    user_id: "user id",
    
    // resource (optional)
    resource_id: "resource id",
    resource_type: "resource type"
};
permifyClient.isAuthorized(payload)
    .then((rule) => {
        if(rule.data.allow){
            // action
        }
    })
    .catch((error) => {
        console.log(error.response)
    });

For more information on how to use the Permify API, please refer to the Permify API Reference.

Permify Documentation

See more

2.0.1

2 years ago

2.0.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago