ios - 使用 soto-cognito-authentication-kit
问题描述
我需要使用 AWS Cognito 实施本机身份验证,并且我正在尝试在我的 iOS 应用程序(客户端)中使用https://github.com/adam-fowler/soto-cognito-authentication-kit 。
我正在努力使用该CognitoAuthenticatable
对象来启动用户名/密码身份验证。
这是我的代码:
class LoginHandler {
func handleLogin(username: String, password: String) {
var eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let data = AWSCognitoContext()
let response = self.authenticatable.authenticate(
username: username,
password: password,
requireAuthenticatedClient: false,
clientMetadata: nil,
context: data,
on: eventLoopGroup.next()
)
response.flatMap { response in
// use response object
}
}
}
class AWSCognitoContext: CognitoContextData {
var contextData: CognitoIdentityProvider.ContextDataType? {
return CognitoIdentityProvider.ContextDataType(
httpHeaders: [],
ipAddress: "",
serverName: "",
serverPath: "")
}
}
该authenticate
方法应该返回EventLoopFuture<CognitoAuthenticateResponse>
- 如何处理
authenticate
方法的响应?我收到错误Generic parameter 'NewValue' could not be inferred
- 如何构造
CognitoContextData
对象。我只想使用 AWS 服务器位置的默认值。
解决方案
authenticate 函数立即返回EventLoopFuture<...>
. 它会在稍后完成,并在完成时使用身份验证的结果。该EventLoopFuture
对象有多种方法来处理此结果。最简单的是whenComplete
。您可以执行以下操作
response.whenComplete { result in
switch result {
case .failure(let error):
process error ...
case .success(let response):
process authenticate response
}
}
map
如果要处理响应对象,可以使用。例如
response.map { response -> NextObject in
return CreateNextObject(from: response)
}
flatMap
如果要将多个链接EventLoopFutures
在一起,则可以使用。例如
response.flatMap { response -> EventLoopFuture<NextObject> in
return CreateEventLoopFutureNextObject(from: response)
}
如果您遇到could not be inferred
错误问题,最好明确说明您的闭包返回的内容。
swift nio 文档将为您提供更多信息https://apple.github.io/swift-nio/docs/current/NIO/Classes/EventLoopFuture.html
上下文数据用于向 Cognito 提供此身份验证请求来自何处的上下文。当requireAuthenticatedClient
为假时,这并没有真正使用。所以提供一个空的上下文是可以的。
您不应该EventLoopGroup
在函数中创建另一件事。它正在创建可能很耗时的线程,然后您必须在所有进程完成后关闭它们。您可以在此处使用 eventLoopGroupauthenticatable.configuration.cognitoIDP.eventLoopGroup
推荐阅读
- php - 按相同的值php对嵌套数组进行分组
- php - 表单提交后设置值
- html - 将html页面连接到vue页面
- amazon-web-services - 如何为特定 IP 自定义 ec2 实例的出站规则
- c# - 从哪里开始计算系列提示的迭代版本的递归等效(家庭作业)
- javascript - 在 Vue.js 中有条件地定位具有 CSS 样式的类
- java - 如何将数据传递到外键,同时传递数据我使用邮递员得到空值?
- python - 如何在colab的谷歌驱动器中打开json文件
- java - 为什么我需要使用 setter 和自定义构造函数?
- javascript - 点击事件函数在整个文档上触发