【问题标题】:Using soto-cognito-authentication-kit使用 soto-cognito-authentication-kit
【发布时间】:2021-05-10 08:20:36
【问题描述】:

我需要使用 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>

  1. 如何处理authenticate方法的响应?我收到错误Generic parameter 'NewValue' could not be inferred
  2. 如何构造CognitoContextData 对象。我只想使用 AWS 服务器位置的默认值。

【问题讨论】:

    标签: ios amazon-cognito soto


    【解决方案1】:

    身份验证函数立即返回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)
    }
    

    如果您想将多个EventLoopFutures 链接在一起,您可以使用flatMap。例如

    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 的另一件事。它正在创建可能很耗时的线程,然后您必须在所有进程完成后关闭它们。你可以在这里使用 eventLoopGroup authenticatable.configuration.cognitoIDP.eventLoopGroup

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 2017-12-24
      • 2021-03-23
      • 2021-05-06
      • 2020-01-19
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 2016-11-16
      相关资源
      最近更新 更多