Authenticate
The authenticate
function plays a crucial role in initializing the SDK. It establishes a secure session and prepares the SDK for subsequent operations. For optimal effectiveness, follow these guidelines before invoking the function:
Authentication Integration Guide
Introduction
Ensure the core module is integrated correctly for successful authentication.
Steps
Verify Module Installation
Check that the authentication module is installed
Configure Settings
Update configuration files with necessary credentials.
Get the API key and Secret Key from Truvideo Support
Generating a Signature
To enhance the security of your API requests, it's necessary to generate a signature if you have an API key and a Secret key. This process ensures that your requests are authenticated and can be trusted by the server. Follow these steps to generate a signature:
Collect Necessary Keys:
Ensure you have your API key and Secret key ready. These are provided when you register for access to the API.
Create the Signature String:
Combine specific parameters from your request into a single string. This usually involves the HTTP method, URL, and any query parameters or headers that are part of the request.
Apply Hash Function:
Use a cryptographic hash function like HMAC with the Secret key to hash the signature string. The specific algorithm (e.g., SHA256) will depend on the API requirements.
Encode the Signature:
The resulting hash should be encoded in base64 to form the final signature.
Attach the Signature to Your Request:
Include the generated signature in the request headers or query parameters, as stipulated by the API documentation.
By following these steps, you can ensure that each request you make is securely signed and authenticated. Always refer to the specific API documentation for detailed implementation guidelines.
// using QuickCrypto library
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 '';
}
}
Test Connection
Run tests to confirm successful integration.
import {
clearAuthentication,
isAuthenticated,
isAuthenticationExpired,
generatePayload,
authenticate,
initAuthentication
} from '@trunpm/truvideo-react-turbo-core-sdk';
/// Authentication process
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);
}
};
Final Steps: Test Your Integration
After implementing the authentication:
Run your application to verify that the SDK session initializes successfully.
Monitor the logs for any issues related to signatures or tokens.
Once confirmed, proceed to explore media capture, uploading, and editing features.
Thank you for using the Truvideo SDK. You're now authenticated and ready to explore all features!
Last updated
Was this helpful?