iOS - AWS Cognito - 检查用户是否已存在

Mik*_*imz 1 amazon-web-services ios swift amazon-cognito

我想允许用户在字段中输入他们的电子邮件地址/密码.继续,我想运行检查以查看该用户是否已存在.如果他们这样做,请将其登录并继续使用应用程序,如果他们不这样做,请转到帐户创建流程,在那里他们将被指示添加姓名,电话号码等.

我不能为我的生活找到有关如何使用AWS Cognito登录用户的文档.我应该能够在电话中传递电子邮件/传递并获得回复,表示用户存在/用户不存在或者其他什么!我在这里错过了什么吗?

任何帮助将不胜感激.我仔细检查了文件......这是我的最后一招.

Chr*_*ute 8

在当前的SDK中,调用getUser你的AWSCognitoIdentityUserPooljust构造内存中的用户对象.要通过网络进行呼叫,您需要getSession在构造的用户上调用该方法.这是我写的一个Swift 3方法,用于检查电子邮件是否可用:

/// Check whether an email address is available.
///
/// - Parameters:
///   - email: Check whether this email is available.
///   - completion: Called on completion with parameter true if email is available, and false otherwise.
func checkEmail(_ email: String, completion: @escaping (Bool) -> Void) {
        let proposedUser = CognitoIdentityUserPoolManager.shared.pool.getUser(email)
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        proposedUser.getSession(email, password: "deadbeef", validationData: nil).continueWith(executor: AWSExecutor.mainThread(), block: { (awsTask) in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            if let error = awsTask.error as? NSError {
                // Error implies login failed. Check reason for failure
                let exceptionString = error.userInfo["__type"] as! String
                if let exception = AWSConstants.ExceptionString(rawValue: exceptionString) {
                    switch exception {
                    case .notAuthorizedException, .resourceConflictException:
                        // Account with this email does exist.
                        completion(false)
                    default:
                        // Some other exception (e.g., UserNotFoundException). Allow user to proceed.
                        completion(true)
                    }
                } else {
                    // Some error we did not recognize. Optimistically allow user to proceed.
                    completion(true)
                }
            } else {
                // No error implies login worked (edge case where proposed email
                // is linked with an account which has password 'deadbeef').
                completion(false)
            }
            return nil
        })
    }
Run Code Online (Sandbox Code Playgroud)

作为参考,我的ExceptionString枚举看起来像这样:

public enum ExceptionString: String {
    /// Thrown during sign-up when email is already taken.
    case aliasExistsException = "AliasExistsException"
    /// Thrown when a user is not authorized to access the requested resource.
    case notAuthorizedException = "NotAuthorizedException"
    /// Thrown when the requested resource (for example, a dataset or record) does not exist.
    case resourceNotFoundException = "ResourceNotFoundException"
    /// Thrown when a user tries to use a login which is already linked to another account.
    case resourceConflictException = "ResourceConflictException"
    /// Thrown for missing or bad input parameter(s).
    case invalidParameterException = "InvalidParameterException"
    /// Thrown during sign-up when username is taken.
    case usernameExistsException = "UsernameExistsException"
    /// Thrown when user has not confirmed his email address.
    case userNotConfirmedException = "UserNotConfirmedException"
    /// Thrown when specified user does not exist.
    case userNotFoundException = "UserNotFoundException"
}
Run Code Online (Sandbox Code Playgroud)