Flutter AWS Cognito身份认证插件amazon_cognito_identity_dart的使用
Flutter AWS Cognito身份认证插件amazon_cognito_identity_dart
的使用
简介
amazon_cognito_identity_dart
是一个基于 Dart 编写的 AWS Cognito 身份认证 SDK。它提供了多种功能,包括用户注册、登录、验证、密码重置等。该库基于官方的 amazon-cognito-identity-js
库,并适用于 Flutter 开发。
注意: 该包目前尚未达到生产就绪状态,但在开发阶段可以使用。
使用案例
使用案例 1:用户注册
通过提供 UserPoolId
和 ClientId
创建 CognitoUserPool
对象,并使用用户名、密码及属性列表完成用户注册。
import 'package:amazon_cognito_identity_dart/cognito.dart';
final userPool = new CognitoUserPool(
'ap-southeast-1_xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
final userAttributes = [
new AttributeArg(name: 'first_name', value: 'Jimmy'),
new AttributeArg(name: 'last_name', value: 'Wong'),
];
var data;
try {
data = await userPool.signUp('email@inspire.my', 'Password001',
userAttributes: userAttributes);
} catch (e) {
print(e);
}
使用案例 2:确认未认证用户的注册
通过接收到的短信或邮件验证码完成用户注册确认。
import 'package:amazon_cognito_identity_dart/cognito.dart';
final userPool = new CognitoUserPool(
'ap-southeast-1_xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
final cognitoUser = new CognitoUser(
'email@inspire.my', userPool);
bool registrationConfirmed = false;
try {
registrationConfirmed = await cognitoUser.confirmRegistration('123456');
} catch (e) {
print(e);
}
print(registrationConfirmed);
使用案例 4:用户登录并获取会话
通过提供用户名和密码完成用户登录,并获取身份认证会话。
import 'package:amazon_cognito_identity_dart/cognito.dart';
final userPool = new CognitoUserPool(
'ap-southeast-1_xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
final cognitoUser = new CognitoUser(
'email@inspire.my', userPool);
final authDetails = new AuthenticationDetails(
username: 'email@inspire.my', password: 'Password001');
CognitoUserSession session;
try {
session = await cognitoUser.authenticateUser(authDetails);
} on CognitoUserNewPasswordRequiredException catch (e) {
// 处理需要新密码的情况
} catch (e) {
print(e);
}
print(session.getAccessToken().getJwtToken());
使用案例 5:获取已认证用户的属性
获取已认证用户的属性列表。
List<CognitoUserAttribute> attributes;
try {
attributes = await cognitoUser.getUserAttributes();
} catch (e) {
print(e);
}
attributes.forEach((attribute) {
print('attribute ${attribute.getName()} has value ${attribute.getValue()}');
});
使用案例 14:注销用户
注销当前用户会话。
await cognitoUser.signOut();
使用案例 15:全局注销用户
全局注销所有设备上的用户会话。
await cognitoUser.globalSignOut();
额外功能
获取 AWS 凭证
通过身份认证会话获取 AWS 凭证,用于签名其他 AWS 请求。
final credentials = new CognitoCredentials(
'ap-southeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', userPool);
await credentials.getAwsCredentials(session.getIdToken().getJwtToken());
print(credentials.accessKeyId);
print(credentials.secretAccessKey);
print(credentials.sessionToken);
S3 文件上传(HTTP Presigned Post)
使用 HTTP Presigned Post 上传文件到 S3。
// 省略部分代码,请参考完整示例
S3 文件下载(带授权)
使用签名请求从 S3 下载文件。
// 省略部分代码,请参考完整示例
GraphQL 请求签名
为 AppSync 的 GraphQL 请求签名。
// 省略部分代码,请参考完整示例
API Gateway & Lambda 请求签名
为 API Gateway 和 Lambda 请求签名。
// 省略部分代码,请参考完整示例
自定义存储实现
使用自定义存储机制持久化用户会话。
class CustomStorage extends CognitoStorage {
String prefix;
CustomStorage(this.prefix);
[@override](/user/override)
Future setItem(String key, value) async {
_storage[prefix+key] = json.encode(value);
return _storage[prefix+key];
}
[@override](/user/override)
Future getItem(String key) async {
if (_storage[prefix+key] != null) {
return json.decode(_storage[prefix+key]);
}
return null;
}
[@override](/user/override)
Future removeItem(String key) async {
return _storage.remove(prefix+key);
}
[@override](/user/override)
Future<void> clear() async {
_storage = {};
}
}
final customStore = new CustomStorage('custom:');
final userPool = new CognitoUserPool(
'ap-southeast-1_xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
storage: customStore);
final cognitoUser = new CognitoUser(
'email@inspire.my', userPool,
storage: customStore);
final authDetails = new AuthenticationDetails(
username: 'email@inspire.my', password: 'Password001');
await cognitoUser.authenticateUser(authDetails);
更多关于Flutter AWS Cognito身份认证插件amazon_cognito_identity_dart的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复