我需要用AWS认知实现本地身份验证,我正在我的iOS应用程序(客户端)中尝试使用iOS。
我很难用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 inferredCognitoContextData对象。我只想对AWS服务器位置使用默认值。发布于 2021-02-06 10:51:16
身份验证函数将立即使用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错误有问题,最好明确说明您的闭包返回的内容。
快速的nio文档将为您提供更多的信息https://apple.github.io/swift-nio/docs/current/NIO/Classes/EventLoopFuture.html。
上下文数据是为认知提供上下文,以了解身份验证请求来自何处。当requireAuthenticatedClient是假的时候,这并不是真正的使用。所以提供一个空的上下文是可以的。
另一件您不应该在函数中创建EventLoopGroup的事情。它正在创建可能耗时的线程,然后当所有进程完成时,您必须关闭它们。您可以在这里使用eventLoopGroup authenticatable.configuration.cognitoIDP.eventLoopGroup
https://stackoverflow.com/questions/66072633
复制相似问题