Flutter身份验证插件humanid_flutter_sdk的使用

Flutter身份验证插件humanid_flutter_sdk的使用


HumanID Flutter SDK


HumanID 是一个匿名的在线身份验证平台,使平台能够提供社交登录的速度和舒适性,同时确保绝对隐私,并通过永久阻止机器人、垃圾邮件和恶意用户来保护我们的社区。


需求

  • Dart
  • Flutter SDK

请更新到最新的稳定版本SDK!


安装

pubspec.yaml 文件中添加以下依赖:

dependencies:
  humanid_flutter_sdk: ^0.0.5

凭证访问

请在 开发者控制台 创建账户并创建应用。


如何使用

初始化 HumanID SDK

在应用的主函数中初始化 HumanID SDK:

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

配置 HumanID SDK

在你想要实现身份验证的页面上放置 HumanID SDK,并不要忘记设置你的客户端ID和客户端密钥。

import 'package:flutter/material.dart';
import 'package:humanid_flutter_sdk/di/injector.dart';
import 'package:humanid_flutter_sdk/presentation/humanid_flutter_sdk.dart';
import 'package:humanid_flutter_sdk/values/country_code.dart';
import 'package:humanid_flutter_sdk/values/supported_language.dart';

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  String token = '';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Human ID Example',
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: const Color(0xFF023B60),
          title: const Text('Flutter Example app'),
        ),
        body: const ExamplePage(),
      ),
    );
  }
}

class ExamplePage extends StatefulWidget {
  const ExamplePage({Key? key}) : super(key: key);

  @override
  State<ExamplePage> createState() => _ExamplePageState();
}

class _ExamplePageState extends State<ExamplePage> {
  String token = '';

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        getTokenFromLogin(token),
        HumanIDSdk(
          language: SupportedLanguage.ENGLISH_US,
          priorityCountry: [
            CountryCode.UNITED_STATES,
            CountryCode.INDONESIA,
            CountryCode.JAPAN,
          ],
          clientId: 'PUT_YOUR_CLIENT_ID',
          clientSecret: 'PUT_YOUR_CLIENT_SECRET',
          wrapperWidget: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12.0),
              color: const Color(0xFF023B60),
            ),
            padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 12),
            child: const Text(
              'Continue with HumanID',
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 14,
                  fontWeight: FontWeight.w700),
            ),
          ),
          onLoginSuccessfully: (exchangedToken) {
            setState(() {
              token = exchangedToken;
            });
          },
        ),
      ],
    );
  }

  Widget getTokenFromLogin(String token) {
    if (token.isNotEmpty) {
      return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Text('Token : $token'),
      );
    } else {
      return Container();
    }
  }
}

设置语言

你可以从 SDK 中选择多种语言之一。例如,设置为英语(美国):

HumanIDSdk(
  language: SupportedLanguage.ENGLISH_US,
)

设置优先国家

你可以设置优先使用的国家列表。例如,设置优先国家为美国、印度尼西亚和日本:

HumanIDSdk(
  priorityCountry: [
    CountryCode.UNITED_STATES,
    CountryCode.INDONESIA,
    CountryCode.JAPAN,
  ],
)

获取交换令牌

当你成功登录时,可以通过回调获取交换令牌:

onLoginSuccessfully: (exchangedToken) {
  setState(() {
    token = exchangedToken;
  });
},

更多关于Flutter身份验证插件humanid_flutter_sdk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter身份验证插件humanid_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用humanid_flutter_sdk进行身份验证的示例代码。这个插件通常用于处理生物特征认证(如人脸识别)或其他形式的身份验证。请注意,实际使用中可能需要根据具体的业务逻辑和API文档进行调整。

首先,确保你的Flutter项目已经创建,并且在pubspec.yaml文件中添加了humanid_flutter_sdk依赖:

dependencies:
  flutter:
    sdk: flutter
  humanid_flutter_sdk: ^最新版本号 # 替换为实际的最新版本号

然后运行flutter pub get来安装依赖。

接下来,在你的Flutter项目中,你可以按照以下步骤使用humanid_flutter_sdk进行身份验证:

  1. 导入插件

在你的Dart文件中导入humanid_flutter_sdk

import 'package:humanid_flutter_sdk/humanid_flutter_sdk.dart';
  1. 初始化SDK

在使用SDK之前,你需要初始化它。这通常涉及到设置API密钥或其他必要的配置。

void initHumanIdSdk() async {
  try {
    // 替换为你的API密钥和其他配置
    String apiKey = "你的API密钥";
    bool result = await HumanIdFlutterSdk.init(apiKey: apiKey);
    if (result) {
      print("HumanID SDK initialized successfully.");
    } else {
      print("Failed to initialize HumanID SDK.");
    }
  } catch (e) {
    print("Error initializing HumanID SDK: $e");
  }
}
  1. 开始身份验证

一旦SDK初始化成功,你可以调用相关方法来启动身份验证流程。

void startAuthentication() async {
  try {
    // 启动身份验证流程
    HumanIdAuthenticationResult result = await HumanIdFlutterSdk.startAuthentication();
    if (result.isSuccess) {
      print("Authentication successful: ${result.data}");
      // 处理成功的情况,比如保存token或用户信息
    } else {
      print("Authentication failed: ${result.errorMessage}");
      // 处理失败的情况
    }
  } catch (e) {
    print("Error during authentication: $e");
  }
}
  1. 在UI中调用这些方法

你可以在你的Flutter UI中通过按钮或其他交互元素来触发这些身份验证流程。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('HumanID Flutter SDK Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: () async {
                  // 初始化SDK
                  await initHumanIdSdk();
                  // 开始身份验证
                  await startAuthentication();
                },
                child: Text('Start Authentication'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

请注意,上述代码是一个简化的示例,实际使用中可能需要根据humanid_flutter_sdk的具体API和文档进行更详细的配置和处理。特别是错误处理、用户隐私保护、以及API密钥的安全存储等方面需要特别注意。

此外,由于humanid_flutter_sdk可能是一个第三方库,其API和用法可能会随着版本的更新而变化,因此建议始终参考最新的官方文档来获取最准确的信息。

回到顶部