Flutter身份验证流程插件authenteq_flow的使用
Flutter身份验证流程插件authenteq_flow的使用
authenteq_flow
是一个用于 Flutter 应用的身份验证流程插件。通过该插件,可以集成 Authenteq 的身份验证服务,实现用户身份验证和面部识别等功能。
使用步骤
要使用 authenteq_flow
插件,首先需要在 pubspec.yaml
文件中添加依赖项,并确保已经安装了所有依赖。
添加依赖
在 pubspec.yaml
文件中添加以下依赖:
dependencies:
authenteq_flow: ^版本号
然后运行 flutter pub get
来获取依赖。
初始化插件
接下来,在你的应用中初始化并使用插件。以下是完整的示例代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:authenteq_flow/authenteq_flow.dart';
import 'package:authenteq_flow/models/IdentificationParameters.dart';
import 'package:authenteq_flow/models/IdentificationResult.dart';
import 'package:authenteq_flow/models/FaceAuthenticationParameters.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _version = '未知';
dynamic _identificationResult;
String? _faceAuthResult;
Exception? _exception;
final String _clientId = '< 我的客户端ID >';
final String _clientSecret = '< 我的秘密密钥 >';
[@override](/user/override)
void initState() {
super.initState();
loadSdkVersion();
}
Future<void> loadSdkVersion() async {
String version;
try {
version = await AuthenteqFlow.getVersion;
} on Exception {
version = '未能获取SDK版本。';
}
if (!mounted) return;
setState(() {
_version = version;
});
}
Future<void> startIdentification() async {
IdentificationResult? result;
Exception? exception;
try {
IdentificationParameters parameters = IdentificationParameters();
parameters.clientId = _clientId;
parameters.clientSecret = _clientSecret;
// parameters.flowId = 'default-kyc-mobile'; // 可选参数
parameters.theme = {
'primaryColor': '#00a2ff',
'AndroidStyle': 'AuthenteqCustom',
'identificationInstructionImageForDriverLicense': 'graphics/driver.png'
};
result = await AuthenteqFlow.identification(parameters);
} on Exception catch(e) {
exception = e;
}
setState(() {
_identificationResult = result;
_exception = exception;
});
}
Future<void> faceAuthentication() async {
String? result;
Exception? exception;
try {
FaceAuthenticationParameters parameters = FaceAuthenticationParameters();
parameters.clientId = _clientId;
parameters.clientSecret = _clientSecret;
parameters.theme = {
'primaryColor': '#00a2ff',
'AndroidStyle': 'AuthenteqCustom'
};
parameters.verificationId = _identificationResult?.verificationId;
result = await AuthenteqFlow.faceAuthentication(parameters);
} on Exception catch(e) {
exception = e;
}
setState(() {
if (result != null) {
checkFaceAuthentication(result);
}
_exception = exception;
});
}
Future<void> checkFaceAuthentication(String code) async {
String? contentsResult;
Exception? exception;
try {
final basicAuth = base64.encode(utf8.encode('$_clientId:$_clientSecret'));
final queryParameters = { 'code': code };
final uri = Uri.https('api.app.authenteq.com', '/mobile-sdk/face-authentication-result', queryParameters);
final request = await HttpClient().getUrl(uri);
request.headers.set(HttpHeaders.authorizationHeader, 'Basic $basicAuth');
var response = await request.close();
await for (var contents in response.transform(const Utf8Decoder())) {
var parsedJson = jsonDecode(contents);
contentsResult = parsedJson['success'].toString().toUpperCase();
}
} on Exception catch(e) {
exception = e;
}
setState(() {
_faceAuthResult = contentsResult;
_exception = exception;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Authenteq Flow 插件示例'),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Spacer(),
ElevatedButton(
onPressed: () {
startIdentification();
},
child: const Text('开始身份验证'),
),
ElevatedButton(
onPressed: () {
faceAuthentication();
},
child: const Text('面部认证'),
),
Text('正在使用 Authenteq SDK: $_version\n'),
const Spacer(),
exceptionWidget(),
identificationResultWidget(),
faceAuthResultWidget()
],
),
),
),
);
}
Widget exceptionWidget() {
return _exception == null ? Container() : Text(_exception.toString(), style: const TextStyle(color: Colors.red));
}
Widget identificationResultWidget() {
return _identificationResult == null ? Container() : Text(
"验证ID: ${_identificationResult.verificationId}\n"
);
}
Widget faceAuthResultWidget() {
return _faceAuthResult == null ? Container() : Text(
"面部认证成功: $_faceAuthResult\n"
);
}
}
代码解释
-
导入依赖:
import 'package:authenteq_flow/authenteq_flow.dart'; import 'package:authenteq_flow/models/IdentificationParameters.dart'; import 'package:authenteq_flow/models/IdentificationResult.dart'; import 'package:authenteq_flow/models/FaceAuthenticationParameters.dart';
-
初始化:
void main() { runApp(const MyApp()); }
-
状态管理:
class _MyAppState extends State<MyApp> { String _version = '未知'; dynamic _identificationResult; String? _faceAuthResult; Exception? _exception; final String _clientId = '< 我的客户端ID >'; final String _clientSecret = '< 我的秘密密钥 >';
-
加载SDK版本:
Future<void> loadSdkVersion() async { String version; try { version = await AuthenteqFlow.getVersion; } on Exception { version = '未能获取SDK版本。'; } if (!mounted) return; setState(() { _version = version; }); }
-
启动身份验证:
Future<void> startIdentification() async { IdentificationResult? result; Exception? exception; try { IdentificationParameters parameters = IdentificationParameters(); parameters.clientId = _clientId; parameters.clientSecret = _clientSecret; // parameters.flowId = 'default-kyc-mobile'; // 可选参数 parameters.theme = { 'primaryColor': '#00a2ff', 'AndroidStyle': 'AuthenteqCustom', 'identificationInstructionImageForDriverLicense': 'graphics/driver.png' }; result = await AuthenteqFlow.identification(parameters); } on Exception catch(e) { exception = e; } setState(() { _identificationResult = result; _exception = exception; }); }
-
面部认证:
Future<void> faceAuthentication() async { String? result; Exception? exception; try { FaceAuthenticationParameters parameters = FaceAuthenticationParameters(); parameters.clientId = _clientId; parameters.clientSecret = _clientSecret; parameters.theme = { 'primaryColor': '#00a2ff', 'AndroidStyle': 'AuthenteqCustom' }; parameters.verificationId = _identificationResult?.verificationId; result = await AuthenteqFlow.faceAuthentication(parameters); } on Exception catch(e) { exception = e; } setState(() { if (result != null) { checkFaceAuthentication(result); } _exception = exception; }); }
-
检查面部认证结果:
Future<void> checkFaceAuthentication(String code) async { String? contentsResult; Exception? exception; try { final basicAuth = base64.encode(utf8.encode('$_clientId:$_clientSecret')); final queryParameters = { 'code': code }; final uri = Uri.https('api.app.authenteq.com', '/mobile-sdk/face-authentication-result', queryParameters); final request = await HttpClient().getUrl(uri); request.headers.set(HttpHeaders.authorizationHeader, 'Basic $basicAuth'); var response = await request.close(); await for (var contents in response.transform(const Utf8Decoder())) { var parsedJson = jsonDecode(contents); contentsResult = parsedJson['success'].toString().toUpperCase(); } } on Exception catch(e) { exception = e; } setState(() { _faceAuthResult = contentsResult; _exception = exception; }); }
-
UI展示:
[@override](/user/override) Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Authenteq Flow 插件示例'), ), body: Center( child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ const Spacer(), ElevatedButton( onPressed: () { startIdentification(); }, child: const Text('开始身份验证'), ), ElevatedButton( onPressed: () { faceAuthentication(); }, child: const Text('面部认证'), ), Text('正在使用 Authenteq SDK: $_version\n'), const Spacer(), exceptionWidget(), identificationResultWidget(), faceAuthResultWidget() ], ), ), ), ); }
-
异常处理:
Widget exceptionWidget() { return _exception == null ? Container() : Text(_exception.toString(), style: const TextStyle(color: Colors.red)); }
-
显示身份验证结果:
Widget identificationResultWidget() { return _identificationResult == null ? Container() : Text( "验证ID: ${_identificationResult.verificationId}\n" ); }
-
显示面部认证结果:
Widget faceAuthResultWidget() { return _faceAuthResult == null ? Container() : Text( "面部认证成功: $_faceAuthResult\n" ); }
更多关于Flutter身份验证流程插件authenteq_flow的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter身份验证流程插件authenteq_flow的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用authenteq_flow
插件来实现身份验证流程的示例代码。这个插件通常用于集成Authenteq的身份验证服务。请注意,实际使用时你需要替换示例中的API密钥和其他配置信息。
首先,确保你已经在pubspec.yaml
文件中添加了authenteq_flow
依赖:
dependencies:
flutter:
sdk: flutter
authenteq_flow: ^最新版本号 # 替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
接下来是示例代码,展示如何使用authenteq_flow
插件:
import 'package:flutter/material.dart';
import 'package:authenteq_flow/authenteq_flow.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Authenteq Flow Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: AuthenteqFlowDemo(),
);
}
}
class AuthenteqFlowDemo extends StatefulWidget {
@override
_AuthenteqFlowDemoState createState() => _AuthenteqFlowDemoState();
}
class _AuthenteqFlowDemoState extends State<AuthenteqFlowDemo> {
final AuthenteqFlow _authenteqFlow = AuthenteqFlow(
apiKey: '你的API密钥', // 替换为你的API密钥
clientId: '你的客户端ID', // 替换为你的客户端ID
redirectUri: '你的重定向URI', // 替换为你的重定向URI
environment: AuthenteqEnvironment.production, // 或者 AuthenteqEnvironment.sandbox
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Authenteq Flow Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
try {
// 触发身份验证流程
final AuthenteqResult result = await _authenteqFlow.startFlow(context);
if (result.status == AuthenteqStatus.success) {
// 身份验证成功,处理用户数据
print('User data: ${result.userData}');
// 可以将用户数据保存到本地或进行其他处理
} else if (result.status == AuthenteqStatus.error) {
// 处理错误
print('Error: ${result.errorMessage}');
} else if (result.status == AuthenteqStatus.canceled) {
// 用户取消了身份验证流程
print('User canceled the flow.');
}
} catch (e) {
// 处理其他异常
print('Exception: $e');
}
},
child: Text('Start Authenteq Flow'),
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮,当用户点击该按钮时,将启动Authenteq的身份验证流程。身份验证流程完成后,根据结果(成功、错误或取消),我们打印相应的信息。
注意事项:
- API密钥和配置:在实际使用中,请确保替换
apiKey
、clientId
和redirectUri
为你的实际值。 - 环境:根据你的需求,选择
AuthenteqEnvironment.production
或AuthenteqEnvironment.sandbox
。 - 用户数据处理:在身份验证成功后,你可以根据需要对
result.userData
进行处理,例如保存到本地数据库或发送到服务器。
这个示例代码提供了一个基本的框架,你可以根据实际需求进一步扩展和定制。