Using the Agent with Node.js SDK

Frontegg Documentation

🚧

Prerequisites

  • Note that Configuring the Agent is required in order to proceed with the SDK installation.
  • The SDK is currently supported in Node.js.

Installation

To install the package using npm, run the following command:


$ npm install @frontegg/e10s-client

Initializing the client

import { EntitlementsClientFactory, RequestContextType } from '@frontegg/e10s-client';

const e10sClient = EntitlementsClientFactory.create({
	pdpHost: 'localhost:8181' // Entitlements Agent Host
});

Setting up the Subject Context

Subject context describes the user which performs the action, these can be taken from Frontegg JWT if authenticating with Frontegg

const subjectContext: SubjectContext = {
	tenantId: 'my-tenant-id',
	userId: 'my-user-id', // Optional
	permissions: ['read', 'write'], // Optional
	attributes: { 'my-custom-attribute': 'some-value' } // Optional
};

Query

The Entitlements client allows you to query for a feature, permission or a route entitlement, each requires different context information.

Query for Feature

const e10sResult = await e10sClient.isEntitledTo(
	subjectContext,
	{
		type: RequestContextType.Feature,
		featureKey: 'my-cool-feature'
	}
);

if (!e10sResult.result) {
	console.log(`User is not entitled to "my-cool-feature" feature, reason: ${e10sResult.justification}`);
}

Query for Permission

const e10sResult = await e10sClient.isEntitledTo(
	subjectContext,
	{
		type: RequestContextType.Permission,
		permissionKey: 'read'
	}
);

if (!e10sResult.result) {
	console.log(`User is not entitled to "read" permission, reason: ${e10sResult.justification}`);
}

Query for Route

const e10sResult = await e10sClient.isEntitledTo(
	subjectContext,
	{
		type: RequestContextType.Route,
		method: "GET",
        path: "/users"
	}
);

if (!e10sResult.result) {
	console.log(`User is not entitled to "GET /users" route, reason: ${e10sResult.justification}`);
}

Justifications

List of possible justifications

JustificationMeaning
MISSING_FEATUREUser is missing the feature
MISSING_PERMISSIONUser is missing the permission
PLAN_EXPIREDUser has a plan that covers the feature, but the plan is expired
MISSING_ROUTERequested route is not configured
ROUTE_DENIEDRequested route is configured to be blocked

Monitoring

In case monitoring mode is enabled, the result object will always return as follows (and the Entitlement check result will be 'logged'):

{
	"result": true,
	"monitoring": true
}