Core

Import the following dependency in your file, here you will use these function.

import { 
    isAuthenticated,
    isAuthenticationExpired,
    generatePayload,
    authenticate,
    initAuthentication,
    clearAuthentication 
} from 'TruVideoReactCoreSdk';

Authentication Status

The Truvideo SDK provides two properties that serve to check the authentication status:

  • isAuthenticated() : Returns true or false indicating whether the client is authenticated. Please note that authentication may be expired.

  • isAuthenticationExpired() : Return true or false indicating whether the authentication is expired. If the client is not authenticated, it always returns false.

const isAuth = await isAuthenticated();
const isAuthExpired = await isAuthenticationExpired();

How to authenticate

The authenticate method requires the following inputs:

  1. ApiKey: A string provided by Truvideo upon registration.

  2. Payload: Essential data containing device information, generated internally using the generatePayload() function.

  3. Signature: Encrypt the payload with the provided secret key using the SHA256 algorithm to regenerate the signature.

Upon completion, it triggers the onReady callback or the onError callback in case of an error.

Note
The API key and Secret key are provided by the TRUVideo SDK team.

Algorithm for encoding the signature

The encoding algorithm operates by taking the secret key and payload as inputs, generating an encoded string suitable for authentication purposes.

const toSha256String = (signature: any, payload: any) => {
    try {
      // Create HMAC using 'sha256' and the provided signature as the key
      const hmac = QuickCrypto.createHmac('sha256', signature);
      // Update the HMAC with the payload
      hmac.update(payload);
      // Generate the HMAC digest and convert it to a hex string
      const hash = hmac.digest('hex');
      return hash;
    } catch (error) {
      console.error('Error generating SHA256 string:', error);
      return '';
    }
}
Note 
We suggest avoiding storing the secret within the host application. Instead, set up a private backend owned by you. This backend will handle the payload from the Truvideo SDK and generate the signature. By doing this, you can prevent the secret from being exposed, reducing the risk of security issues
const authFunc = async () => {
    try {
      const isAuth = await isAuthenticated();
      console.log('isAuth', isAuth);
      // Check if authentication token has expired
      const isAuthExpired = await isAuthenticationExpired();
      console.log('isAuthExpired', isAuthExpired);
      //generate payload for authentication
      const payload = await generatePayload();
      const apiKey = "YOUR-API-KEY";
      const secretKey = "YOUR-SECRET-KEY";
      const signature = await toSha256String(secretKey, payload);
      const externalId = ""
      // Authenticate user
      if (!isAuth || isAuthExpired) {
        await authenticate(apiKey, payload, signature, externalId);
      }
      // If user is authenticated successfully
      const initAuth =  await initAuthentication();
      console.log('initAuth', initAuth);
    } catch (error) {
      console.log('error', error);
    }
  };

Clear Authentication

To delete the current session and erase all associated authentication data, utilize the clearAuthentication() method

const clearAuth = await clearAuthentication();

Last updated

Was this helpful?