Aserto single-page application javascript SDK
Loosely modeled after the Auth0 SPA SDK.
Installation
Using npm:
npm install @aserto/aserto-spa-js
Using yarn:
yarn add @aserto/aserto-spa-js
Getting Started
Creating the client
Create an AsertoClient
instance before rendering or initializing your application. You should only have one instance of the client.
You need a valid access token before you can instantiate the client. For
the next few examples, the accessToken
variable is assumed to contain a
valid access token.
To obtain one via Auth0 (for example), use code like this:
import createAuth0Client from '@auth0/auth0-spa-js';
const auth0 = await createAuth0Cient(
domain: '<AUTH0_DOMAIN>',
client_id: '<AUTH0_CLIENT_ID>',
redirect_uri: '<MY_CALLBACK_URL>'
);
const accessToken = await auth0.getTokenSilently();
Create an AsertoClient
in the following way:
import createAsertoClient from '@aserto/aserto-spa-js';
const aserto = await createAsertoClient({
accessToken: accessToken,
serviceUrl: 'https://service-url',
endpoint: '/__accessmap',
defaultMap: {
visible: true,
enabled: true,
allowed: false
}
});
import { AsertoClient } from '@aserto/aserto-spa-js';
const aserto = new AsertoClient({
accessToken: accessToken,
serviceUrl: 'https://service-url',
endpoint: '/__accessmap'
});
await aserto.reload();
Usage
accessMap()
Retrieves a javascript object that holds the access map
console.log(aserto.accessMap());
resourceMap('path')
Retrieves a map associated with a specific resource.
The path
argument is in the form /path/to/resource
. It may contain a __id
component to indicate an parameter - for example, /mycars/__id
.
The returned map will be in the following format:
{
GET: {
visible: true,
enabled: false,
allowed: false
},
POST: {
visible: true,
enabled: false,
allowed: false
},
PUT: {
},
DELETE: {
}
}
Check whether a verb / path combination is visible and enabled:
const path = '/api/path';
const resource = aserto.resourceMap(path));
const isVisible = resource.GET.visible;
const isEnabled = resource.GET.enabled;
Display the values for all access levels on each verb for the path:
const path = '/api/path';
const resource = aserto.resourceMap(path));
for (const verb of ['GET', 'POST', 'PUT', 'DELETE']) {
for (const access of ['visible', 'enabled', 'allowed']) {
console.log(`${verb} ${path} ${access} is ${resource[verb][access]}`);
}
}