Setup SDK
The Eniblock SDK uses an access token provided by your application to manage user identities. This structure helps ensure user privacy as it prevents the SDK from storing or handling any private user information. Therefore, the crucial responsibility of your application is to provide and validate the access token.
Prerequisite
Before embedding the Eniblock SDK into your application, ensure you have an Identity and Access Management (IAM) system capable of supporting JSON Web Token (JWT) and JSON Web Token (JWT) Profile for OAuth 2.0 Access Tokens. Some renowned IAM systems include:
Follow this tutorial: How to Create a New Auth0 Application
Configuring the SDK
The next step involves initializing the SDK. This process includes supplying your appId
and the accessTokenProvider
function in the configuration:
import { Eniblock} from '@eniblock/sdk';const sdk = new Eniblock({appId: "your-appId",accessTokenProvider: getAccessToken,});
Step 1 - Application Registration
The first step involves registering your application, which essentially allows the SDK to obtain specific credentials to secure access to its features.
To generate your appId
, visit the Eniblock SDK Admin Console.
![]() |
---|
Follow these steps to complete the registration:
- Click on the 'Add Application' button.
- Optionally, provide a Description for your application to offer more context.
- Enter the JWKS (JSON Web Key Set) URI, which is a collection of cryptographic keys used for validating JWTs issued by your authorization server.
- Specify the Domain Name where your application resides.
- Define the Audience, indicating who the intended token recipients are within your application.
Each of these parameters plays a crucial role in establishing trust between the SDK and your application, granting you secure and customized access to various SDK features.
Step 2 - Developing an Access Token Provider
Your application must implement a method for retrieving and renewing access tokens for your end users. The SDK utilizes these tokens to authenticate user requests. Here's a sample implementation:
// Function to manage access token retrieval and renewalconst getAccessToken = async () => {let accessToken = retrieveAccessTokenFromStorage();if (!accessToken) {accessToken = await createNewAccessToken();storeAccessTokenInStorage(accessToken);} else if (isAccessTokenExpired(accessToken)) {accessToken = await renewAccessToken(accessToken);storeAccessTokenInStorage(accessToken);}return accessToken;};
This function manages key operations like retrieving, creating, storing, verifying the expiration of, and renewing access tokens. Remember to replace these function placeholders with your actual implementations.
Step 3 - Validating your Access Token and AppId
As part of the SDK initialization process, it verifies the provided appId
and access token. Should the appId
prove to be invalid or the accessTokenProvider
fails to supply a legitimate token, the system will return an error.
Proper management of these potential issues is crucial to prevent your application from experiencing a crash and to ensure your users receive informative error messages. Below, we provide an example of how such errors could be handled. For a quick test of your configuration, substitute "your appId" and "your accessToken" with your own appId
and access token:
import { Eniblock} from '@eniblock/sdk';try {// Attempt to instantiate the SDKconst sdk = new Eniblock({appId: "your appId",accessTokenProvider: async () => "your accessToken",});} catch (error) {// Log the error for debuggingconsole.error(JSON.stringify(error));// Determine the nature of the error and handle appropriatelyif (error.message.includes("invalid appId")) {// Handle invalid appIdconsole.error("The provided appId is invalid. Please verify your configuration.");} else if (error.message.includes("invalid token")) {// Handle invalid tokenconsole.error("The provided access token is invalid. Please check the functionality of your accessTokenProvider.",);} else {// Handle other types of errorsconsole.error("An unexpected error occurred while initializing the Eniblock SDK.");}}
This robust error handling mechanism enables quick identification and resolution of issues, thus enhancing the stability of your application and the overall user experience.