Package Exports
- @warrantdev/warrant-node
- @warrantdev/warrant-node/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@warrantdev/warrant-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Warrant Node.js Library
Use Warrant in server-side Node.js projects.
Installation
Use npm to install the Warrant module:
npm install @warrantdev/warrant-nodeUsage
Import the Warrant client and pass your API key to the constructor to get started:
const Warrant = require("@warrantdev/warrant-node");
const warrantClient = new Warrant.Client("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");Or using ES modules:
import { Client as WarrantClient } from "@warrantdev/warrant-node";
const warrantClient = new WarrantClient("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");createUser(userId, username)
This method creates a user entity in Warrant with the specified userId. Provide an optional username to make it easier to identify users in the Warrant dashboard.
const Warrant = require("@warrantdev/warrant-node");
const warrantClient = new Warrant.Client("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
// Creates a user with user.id as the userId
warrantClient
.createUser(user.id, user.email)
.then((newUser) => console.log(newUser))
.catch((error) => console.log(error));Or using ES modules and async/await:
import { Client as WarrantClient } from "@warrantdev/warrant-node";
const warrantClient = new WarrantClient("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
// Creates a user with user.id as the userId and
// assigns the new user the "store_owner" role
const newUser = await warrantClient.createUser(user.id, user.email);createWarrant(objectType, objectId, relation, user)
This method creates a warrant which specifies that the provided user (or userset) has relation on the object of type objectType with id objectId.
const Warrant = require("@warrantdev/warrant-node");
const warrantClient = new Warrant.Client("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
// Create a warrant allowing user.id to "view" the store with id store.id
warrantClient
.createWarrant("store", store.id, "view", { userId: user.id })
.then((newWarrant) => console.log(newWarrant))
.catch((error) => console.log(error));Or using ES modules and async/await:
import { Client as WarrantClient } from "@warrantdev/warrant-node";
const warrantClient = new WarrantClient("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
// Create a warrant allowing user.id to "view" the store with id store.id
const newUser = await warrantClient.createWarrant("store", store.id, "view", user.id);createSession(userId)
This method creates a session in Warrant for the user with the specified userId and returns a session token which can be used to make authorized requests to the Warrant API only for the specified user. This session token can safely be used to make requests to the Warrant API's authorization endpoint to determine user access in web and mobile client applications.
const Warrant = require("@warrantdev/warrant-node");
const warrantClient = new Warrant.Client("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
// Creates a session token scoped to the specified userId
// Return this token to your client application to allow
// it to make requests for the given user.
warrantClient
.createSession(user.id)
.then((sessionToken) => console.log(sessionToken))
.catch((error) => console.log(error));Or using ES modules and async/await:
const { Client as WarrantClient } = require("@warrantdev/warrant-node");
const warrantClient = new WarrantClient("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
// Creates a session token scoped to the specified userId
// Return this token to your client application to allow
// it to make requests for the given user.
const sessionToken = await warrantClient.createSession(user.id);isAuthorized(objectType, objectId, relation, userId)
This method returns a Promise that resolves with true if the user with the specified userId has the specified relation to the object of type objectType with id objectId and false otherwise.
const Warrant = require("@warrantdev/warrant-node");
const warrantClient = new Warrant.Client("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
//
// Example Scenario:
// An e-commerce website where Store Owners can edit store info
//
warrantClient
.isAuthorized("store", storeId, "edit", user.id)
.then((isAuthorized) => {
if (isAuthorized) {
// Carry out logic to allow user to edit a Store
}
})
.catch((error) => console.log(error));Or using ES modules and async/await:
import { Client as WarrantClient } from "@warrantdev/warrant-node";
const warrantClient = new WarrantClient("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
//
// Example Scenario:
// An e-commerce website where Store Owners can edit store info
//
if (await warrantClient.isAuthorized("store", storeId, "edit", user.id)) {
// Carry out logic to allow user to edit a Store
}NOTE: To ignore the objectId when performing authorization calls using isAuthorized, you can pass the constant WARRANT_IGNORE_ID. You must have a corresponding warrant that grants access to ANY user on the given objectType for this check to succeed.
import { Client as WarrantClient, WARRANT_IGNORE_ID } from "@warrantdev/warrant-node";
const warrantClient = new WarrantClient("api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=");
//
// Example Scenario:
// An e-commerce website where Store Owners can edit store info
//
if (await warrantClient.isAuthorized("store", WARRANT_IGNORE_ID, "edit", user.id)) {
// Carry out logic to allow user to edit a Store
}We’ve used a random API key in these code examples. Replace it with your actual publishable API keys to test this code through your own Warrant account.
For more information on how to use the Warrant API, please refer to the Warrant API reference.
Note that we may release new minor and patch versions of
@warrantdev/warrant-node with small but backwards-incompatible fixes to the type
declarations. These changes will not affect Warrant itself.
TypeScript support
This package includes TypeScript declarations for Warrant.