Aserto React SDK
Loosely modeled after the Auth0 React SDK.
This SDK uses the Aserto javascript SPA SDK.
Installation
Using npm:
npm install @aserto/aserto-react
Using yarn:
yarn add @aserto/aserto-react
Getting Started
Configure the SDK by wrapping your application in AsertoProvider
. If using in conjunction with the Auth0Provider
, AsertoProvider
should be nested inside of it.
import React from 'react'
import ReactDOM from 'react-dom'
import { AsertoProvider } from '@aserto/aserto-react'
import { Auth0Provider } from '@auth0/auth0-react'
import App from './App'
ReactDOM.render(
<Auth0Provider
domain="YOUR_AUTH0_DOMAIN"
clientId="YOUR_AUTH0_CLIENT_ID"
redirectUri={window.location.origin}
>
<AsertoProvider>
<App />
</AsertoProvider>
</Auth0Provider>,
document.getElementById('app')
);
Use the useAserto
hook in your components to initialize (init
), reload the display state map (reload
) or to access its state (loading
, displayStateMap
, getDisplayState
, etc):
import React from 'react'
import { useAserto } from '@aserto/aserto-react'
import { useAuth0 } from '@auth0/auth0-react'
function App() {
const {
loading,
isLoaded,
error,
identity,
setIdentity,
displayStateMap,
getDisplayState,
init,
reload
} = useAserto();
const { isLoading, isAuthenticated, getAccessTokenSilently } = useAuth0();
useEffect(() => {
async function load() {
const token = await getAccessTokenSilently();
if (token) {
await init({ accessToken: token });
}
}
if (!isLoading && isAuthenticated) {
load();
}
}, [isAuthenticated]);
if (loading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error}</div>;
} else {
return (
<div>
{
// output the display state map as a string
displayStateMap
}
</div>
);
}
}
export default App
init(options, body)
Initialize the Aserto client using the (required) options
map.
If the body
parameter is passed in, it is passed through to the AsertoClient
instance that will retrieve the display state map from the API endpoint (and used as the resource context for the decisiontree
API call).
const { init, displayStateMap } = useAserto();
await init({
serviceUrl: 'http://service-url',
endpointName: '/__displaystatemap',
accessToken: '<VALID ACCESS TOKEN>',
throwOnError: true,
defaultDisplayState: {
visible: false,
enabled: false
}
});
console.log(displayStateMap);
reload(body, headers)
Re-load the display state map for a service that exposes it.
If the body
parameter is passed in, it is passed through to the AsertoClient
instance that will retrieve the display state map from the API endpoint (and used as the resource context for the decisiontree
API call).
If the headers
parameter is passed in, it is likewise passed through to the AsertoClient
instance that will retrieve the display state map from the API endpoint.
Note: init()
must be called before reload()
.
const { reload, displayStateMap } = useAserto();
await reload();
console.log(displayStateMap);
identity and setIdentity
setIdentity
can be used to set the identity to pass as an identity
HTTP header. It will override an identity
header that is passed into reload(headers)
. This is the preferred way to send an identity to the displayStateMap API, which can be used to override the Authorization header by the displayStateMap
middleware in the express-jwt-aserto
Node.js SDK.identity
will return the current identity (or undefined if it hasn't been set).
getDisplayState('method, 'path')
Retrieves a displayState associated with a specific resource.
By convention, the method
argument is an HTTP method (GET, POST, PUT, DELETE), and the path
argument is in the form /path/to/resource
. It may contain a __id
component to indicate an parameter - for example, /mycars/__id
.
If only the method
argument is passed in, it is assumed to be a key into the displayStateMap
(typically in the form of METHOD/path/to/resource
).
The returned map will be in the following format:
{
visible: true,
enabled: false,
}
Note: init()
must be called before getDisplayState()
.
const { getDisplayState } = useAserto();
const path = '/api/path';
const isVisible = aserto.getDisplayState('GET', path).visible;
const isUpdateEnabled = aserto.getDisplayState('PUT', path).enabled;
for (const verb of ['GET', 'POST', 'PUT', 'DELETE']) {
const resource = aserto.getDisplayState(verb, path));
for (const value of ['visible', 'enabled']) {
console.log(`${verb} ${path} ${value} is ${resource[verb][value]}`);
}
}