@ayetu/sdk-js
v1.0.196
Published
A comprehensive SDK for Ayetu services.
Downloads
19
Readme
Ayetu SDK Documentation
Welcome to the Ayetu API and SDK documentation. This guide provides an overview of how to use the Ayetu API and SDK in both web and mobile contexts.
Installation
The Web SDK can be inslled via npm:
npm i @ayetu/sdk-js
Import
You can import the Web SDK as a named import:
import { Ayetu } from '@ayetu/sdk-js';
Initialisation
Web SDK
The initialization process for the Web SDK is crucial and should only be performed once when the page loads. The SDK exposes an Ayetu.init() function, which initializes the SDK with the required configuration.
Ayetu.init({
net: 'Test',
connectionToken: new URLSearchParams(window.location.search).get('connection-token') || undefined,
updateToken: new URLSearchParams(window.location.search).get('update-token') || undefined,
url: window.location.href,
debug: true,
});
Parameters
- net: Can be set to "Main" or "Test", which adjusts the API endpoint URL accordingly.
- connectionToken: If a connection-token is present in the URL after returning from a social login, it can be used to initialize the SDK and connect the user to the websocket.
- updateToken: If an update-token is present in the URL after returning from a social login, it can be used to initialize the SDK. The auth.status will be set to "RequiresMore", and an 'auth-status-changed' event will be emitted.
- url: Should be set to the current URL or the URL to which you want the user to be returned after signing in with a social provider.
- debug: A boolean value defaulting to false. When set to true, the SDK will provide useful debug logs.
SignIn
To sign in, the user will be redirected to the respective social sign-in provider. If they sign in successfully, they will be returned to the URL specified in Ayetu.init(). The URL will include a query string with either an update-token, connection-token, or error.
Users can Sign in with X:
Ayetu.auth.signIn('X');
Ayetu.auth.signIn('Facebook');
Update Token
After signing in, you will receive an update-token if you have not previously set a Name and a Handle. The name must be at least one character, and the handle must be unique and not used by another user.
Presenting the "Requires More" Screen
- event: The SDK will emit an 'auth-status-changed' event with the value 'RequiresMore'.
- state: Ayetu.state.auth.status will be 'RequiresMore'.
You can check if the entered handle is unique:
const available: boolean = await Ayetu.auth.checkHandle('myhandle');
And submit the update:
const available: boolean = await Ayetu.auth.submit('myhandle', `Alice Boyd`);
You will then observe the status change from 'RequiresMore' to 'Connecting' and then 'Connected'.
Connection Token
Signing in after submitting your name and handle, or as a result of submitting your name and handle, you will receive a connection token. This token is single-use and must be utilized within 10 seconds of issuance.
For the Web SDK, no additional action is required. As soon as you receive a connection token, you will be immediately connected to the websocket. The status will transition from Connecting to Connected.
- event: The SDK will emit an 'auth-status-changed' event with the value 'Connected'.
- state: Ayetu.state.auth.status will be 'Connected'.
Managing the WebSocket Connection
Keeping WebSocket connections open when they are unused can be a waste of resources. Whenever possible and reasonable, close the connection to free up these resources.
Ayetu.auth.disconnect();
Reconnecting
Whenever a connection token is issued, a session-token is set as a secure, HTTP-only cookie. This session-token can be used to obtain a new connection-token whenever needed for reconnection.
The Web handles this for you in two scenarios:
- Initialization (init): If no updateToken or connectionToken is present, an attempt to obtain a new connection-token is made.
- connect: If Ayetu.auth.connect() is called without a connectionToken, an attempt to obtain a new connection-token is made.
Contact Management
Adding a Contact
To add a contact, use the add method provided by the SDK:
const result = Ayetu.contact.add('CON_contactUserId123');
if (!result.success) {
console.error('Failed to add contact:', result.error);
}
Removing a Contact
To remove a contact, use the remove method provided by the SDK:
const result = Ayetu.contact.remove('contactUserId123');
if (!result.success) {
console.error('Failed to remove contact:', result.error);
}
Getting Contacts
To retrieve the list of contacts, use the get method provided by the SDK:
const result = Ayetu.contact.get();
if (!result.success) {
console.error('Failed to retrieve contacts:', result.error);
}