authme_ekyc_sdk 0.1.0 copy "authme_ekyc_sdk: ^0.1.0" to clipboard
authme_ekyc_sdk: ^0.1.0 copied to clipboard

A Flutter package of ekyc of authme.

example/lib/main.dart

import 'dart:developer';

import 'package:authme_ekyc_sdk/ekyc_feature.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

import 'package:authme_ekyc_sdk/ekyc_sdk_plugin_bridge.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter AuthMe eKYC SDK Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const AuthMeHomePage(),
    );
  }
}

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

  @override
  State<AuthMeHomePage> createState() => _AuthMeHomePageState();
}

class _AuthMeHomePageState extends State<AuthMeHomePage> {
  String? _token;
  bool _isLoading = false;
  final AuthmeSdk _authMeSdk = AuthmeSdk();
  late StreamSubscription _resultSubscription;
  String _name = ""; // 這裡是要顯示的名字
  var timestamp = DateTime.now().millisecondsSinceEpoch;

  @override
  void initState() {
    super.initState();
    _resultSubscription = _authMeSdk.resultStream.listen(
          (result) {
        if (result.containsKey('result')) {

          final resultMap = result['result'];

          if (resultMap is Map) {
            // 取得主要資料層
            final data = resultMap['data'];
            // 取得圖片層
            final images = resultMap['images'];

            log("data: $data");

            if (data is Map) {
              // 只修改主要資料層
              Map<String, String> modifications = Map.from(data);

              // 修改需要更新的部分
              modifications['address'] = 'new address';

              // 確認結果時只傳送主要資料層的修改
              _authMeSdk.confirmScanResult(modifications).then((_) {
                log('Scan result confirmed');
              }).catchError((error) {
                log('Failed to confirm scan result: $error');
              });

              // 如果需要處理圖片,可以另外處理
              if (images != null && images is Map<String, dynamic>) {
                final frontImage = images['frontImage'];
                final backImage = images['backImage'];
                // 處理圖片...
              }
            }
          }
        } else if (result.containsKey('error')) {
          // 處理錯誤
          log("錯誤: ${result['error']}");
        }
      },
      onError: (error) {
        print("Stream error: $error");
      },
    );
    _fetchToken();
    initSDK();
  }

  Future<void> _fetchToken() async {
    setState(() {
      _isLoading = true;
    });

    try {
      final response = await http.post(
        Uri.parse('https://stage.auth.authme.com/connect/token'),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: {
          'client_id': 'd7e8cf57499b4c348fbfb4b3e92d0182',
          'client_secret': 'oy9rDBCv5trf6BJe',
          'grant_type': 'client_credentials',
          'scope': 'AuthmeServices customer_id:FlutterTest$timestamp event_name:default',
        },
      );
      print(response);

      if (response.statusCode == 200) {
        final Map<String, dynamic> jsonResponse = json.decode(response.body);
        setState(() {
          _token = jsonResponse['access_token'];
          _isLoading = false;
        });
        _authMeSdk.refreshToken(_token!);
      } else {
        throw Exception('Failed to load token');
      }
    } catch (e) {
      setState(() {
        _isLoading = false;
      });
      print('Error fetching token: $e');
    }
  }

  void initSDK() {
    var serverURL = "https://stage.api.authme.com";
    var activateToken =
        "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmZWF0dXJlcyI6bnVsbCwiZXhwIjoxNzY4MTg0MzM5LCJpc3MiOiJhdXRobWUuY29tIiwibmJmIjotNjIxMzU1OTY4MDB9.WsWXA-mlwe92reX8G5daqWLp19mE0CrmTSLRX8KKxIaeOyX1ijTvMnmgKHJTtJImgDtF7zY_SHnq8AI20cbxH0enGEMU8oVqso5zE4Qba74wqwOwvW1AOfpxX_iaYtJ7MgkZcv7aUhoak6YRo5lvPx1nyRd58J58-LJl5LaBysyGqbrPsNLeEQvHwi18Vs8YykPU1iQvlPbtRm254JIgPnqfZIK83t4AA-g_hZj-iGmUVdRrVNxXx8B96f03GgZIKqx00BYm-jZZBvhJMVuWzNrF7yMcwkPe4QZmDdiX4GfyQd_CAukVXB0nktcIMJ6JIgTMlGwxGdNNSzR8EG8qlw";
    var activateCertificate =
        "-----BEGIN CERTIFICATE-----\nMIIESzCCAzOg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5zdG9yYWdlLmdvb2dsZWFwaXMuY29tL2Q3YzY2NjA2NDVjMDUzOTUx\nNWUyL2NybC5jcmwwDQYJKoZIhvcNAQELBQADggEBABemPM70DFKYJFXkEfBXcM29\n8Y97s8kct3UDPmdCR/n2BMGrhIJZ9/ieIYdcNvQED/GX+Iq9CMiju+0KyJoOKF5e\ndxBVWqKiuaBBQmhWDxQmjdK+/npIoN2CKX6LSdyzm1VPeDgnEKrAmzhTVc3DlP89\ngNoCp+ouxAA622pc1DFkbZzDQMa7TYcYs3bJWDOC9xSNiLXFOemMoh5iImwByacL\n5mK2ppvOsjnFPn9DfY9mM4rLKyLFZRFU4wY9IwDX52PK4z8DVz6D18bcINVZQbmU\nRNFAq4hK9vbwzdozc+XQzYqAQk8ObTDcqHo+JtAgo3Znk9TJyTXvsc0f2SjfzhQ=\n-----END CERTIFICATE-----\n\n";
    _authMeSdk.initialize(
      serverURL,
      activateToken,
      activateCertificate,
    );
  }

  void _startAuthme() async {
    _fetchToken();
    if (_token != null) {
      // await _authMeSdk.startFeature(_token!, AuthmeFeature.TWID);
      // if (platform == 'Android') {
      //   await _authMeService.startAuthMe(_token!);
      // } else {
      //   await AuthMeSdk.startOcrProcess(_token!, "TWNIDCard");
      // }
      // final Map<String, dynamic> result =
      await _authMeSdk.startFeature(_token!, AuthmeFeature.TWID,
          needConfirm: true,
          isResultPageDisplayable: true
      );
      // print("Data: ${result['name']}");
      // setState(() {
      //   _name = result['name'] ?? "No";
      // });
    } else {
      print('Token 不可用');
    }
  }

  void _startAuthmeLiveness() async {
    _fetchToken();
    if (_token != null) {
      await _authMeSdk.startFeature(_token!, AuthmeFeature.Liveness);
      // if (platform == 'Android') {
      //   await _authMeService.startAuthMeLiveness(_token!);
      // } else {
      //   await AuthMeSdk.startLivenessVerification(_token!);
      // }
    } else {
      print('Token 不可用');
    }
  }

  void _startAuthmePassport() async {
    _fetchToken();
    if (_token != null) {
      await _authMeSdk.startFeature(_token!, AuthmeFeature.Passport);
      // if (platform == 'Android') {
      //   await _authMeService.startAuthmePassport(_token!);
      // } else {
      //   await AuthMeSdk.startPassportMRZProcess(_token!);
      // }
    } else {
      print('Token 不可用');
    }
  }

  void _startAuthmePassportNFC() async {
    _fetchToken();
    if (_token != null) {
      await _authMeSdk.startFeature(_token!, AuthmeFeature.NFCPassport);
      // if (platform == 'Android') {
      //   await _authMeService.startAuthmePassportMRZAndNFCProcess(_token!);
      // } else {
      //   await AuthMeSdk.startPassportMRZAndNFCProcess(_token!);
      // }
    } else {
      print('Token 不可用');
    }
  }

  Widget _buildButtonColumn(String title, List<Map<String, Function>> buttons) {
    return Expanded(
      child: Column(
        children: [
          Text(title, style: Theme.of(context).textTheme.titleLarge),
          const SizedBox(height: 20),
          ...buttons.map((button) => Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            child: ElevatedButton(
              onPressed: _token != null ? () => button.values.first() : null,
              child: Text(button.keys.first),
            ),
          )).toList(),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text("Flutter AuthMe eKYC SDK Example"),
      ),
      body: _isLoading
          ? const Center(child: CircularProgressIndicator())
          : Row(
        children: [
          _buildButtonColumn('Sample', [
            {'Start AuthMe': () => _startAuthme()},
            {'Start AuthMe Liveness': () => _startAuthmeLiveness()},
            {'Start Authme Passport': () => _startAuthmePassport()},
            {'Start Authme Passport NFC': () => _startAuthmePassportNFC()},
          ]),
          Text(
              _name.isNotEmpty ? "Name: $_name" : "",
              style: TextStyle(fontSize: 24),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _resultSubscription.cancel();
    _authMeSdk.dispose();
    super.dispose();
  }
}
0
likes
110
points
498
downloads

Publisher

verified publisherauthme.com

Weekly Downloads

A Flutter package of ekyc of authme.

Homepage

Documentation

API reference

License

unknown (license)

Dependencies

flutter, http, plugin_platform_interface

More

Packages that depend on authme_ekyc_sdk