truvideo_core_sdk 1.0.3-beta.3 copy "truvideo_core_sdk: ^1.0.3-beta.3" to clipboard
truvideo_core_sdk: ^1.0.3-beta.3 copied to clipboard

Provides essential user authentication for all SDK modules, ensuring seamless operation across the platform.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:truvideo_core_sdk/truvideo_core_sdk.dart';
import 'package:crypto/crypto.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  bool _isAuthenticated = false;
  bool _isAuthExpired = false;
  bool _isLoading = false;

  final String apiKey = "YOUR_API_KEY";
  final String secretKey = "YOUR_SECRET_KEY";

  @override
  void initState() {
    super.initState();
    initPlatformState();
    authenticate();
  }

  // Initialize platform state
  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await TruvideoCoreSdk.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;
    setState(() {
      _platformVersion = platformVersion;
    });
  }

  // Check authentication status
  Future<void> checkAuthenticationStatus() async {
    try {
      bool isAuthenticated = await TruvideoCoreSdk.isAuthenticated();
      bool isAuthExpired = await TruvideoCoreSdk.isAuthenticationExpired();
      updateAuthState(isAuthenticated, isAuthExpired);
    } catch (e) {
      debugPrint("Error checking authentication status: $e");
      updateAuthState(false, false);
    }
  }

  void updateAuthState(bool isAuthenticated, bool isAuthExpired) {
    if (!mounted) return;
    setState(() {
      _isAuthenticated = isAuthenticated;
      _isAuthExpired = isAuthExpired;
    });
  }

  // Authenticate the user with API key and payload
  Future<void> authenticate() async {
    setState(() {
      _isLoading = true; // Show progress indicator
    });

    try {
      String payload = await TruvideoCoreSdk.generatePayload();
      String signature =
          toSha256String(secret: secretKey, payload: payload) ?? '';

      // Authenticate with generated payload and signature
      await TruvideoCoreSdk.authenticate(
        apiKey: apiKey,
        signature: signature,
        payload: payload,
        externalId: "",
      );
      await TruvideoCoreSdk.initAuthentication();
      checkAuthenticationStatus(); // Refresh status after authentication
    } catch (e) {
      debugPrint('Authentication failed: $e');
    } finally {
      if (mounted) {
        setState(() {
          _isLoading = false; // Hide progress indicator
        });
      }
    }
  }

  // Clear authentication status
  Future<void> clearAuthentication() async {
    try {
      await TruvideoCoreSdk.clearAuthentication();
      checkAuthenticationStatus();
    } catch (e) {
      debugPrint('Clear authentication failed: $e');
    }
  }

  // Generate SHA256 signature using HMAC
  String? toSha256String({required String secret, required String payload}) {
    try {
      // Convert secret and payload to byte arrays
      List<int> secretBytes = utf8.encode(secret);
      List<int> payloadBytes = utf8.encode(payload);
      var hmacSha256 = Hmac(sha256, secretBytes);
      var macData = hmacSha256.convert(payloadBytes);
      return macData.toString();
    } catch (e) {
      debugPrint("Error generating SHA256 HMAC: $e");
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin Example App'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Running on: $_platformVersion\n'),
              Text('Authenticated: $_isAuthenticated\n'),
              Text('Authentication Expired: $_isAuthExpired\n'),
              if (_isLoading) const CircularProgressIndicator(),
              // Show progress bar if loading
              ElevatedButton(
                onPressed: _isLoading ? null : authenticate,
                // Disable when loading
                child: const Text('Authenticate'),
              ),
              ElevatedButton(
                onPressed: clearAuthentication,
                child: const Text('Clear Authentication'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
1
likes
0
points
315
downloads

Publisher

verified publishertruvideo.com

Weekly Downloads

Provides essential user authentication for all SDK modules, ensuring seamless operation across the platform.

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on truvideo_core_sdk

Packages that implement truvideo_core_sdk