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
), load the access map (loadAccessMap
) or to access its state (loading
, accessMap
):
import React from 'react'
import { useAserto } from '@aserto/aserto-react'
import { useAuth0 } from '@auth0/auth0-react'
function App() {
const {
loading,
accessMap,
init,
loadAccessMap
} = useAserto();
const { isLoading, error, isAuthenticated, getAccessTokenSilently } = useAuth0();
useEffect(() => {
async function load() {
const token = await getAccessTokenSilently();
if (token) {
init({ accessToken: token });
await loadAccessMap();
}
}
if (!error && !isLoading && isAuthenticated) {
load();
}
}, [isLoading, isAuthenticated, error]);
if (loading) {
return <div>Loading...</div>;
}
if (!accessMap) {
return <div>Loading...</div>;
} else {
return (
<div>
{
// display the access map as a string
accessMap
}
</div>
);
}
}
export default App
Initialize the Aserto client
const { init } = useAserto();
init({
serviceUrl: 'http://service-url',
endpointName: '/__accessmap',
accessToken: '<VALID ACCESS TOKEN>'
});
Get the access map for a service that exposes it
const { loadAccessMap } = useAserto();
await loadAccessMap();