我想允许用户在字段中输入他们的电子邮件地址/密码。继续后,我想检查一下该用户是否已经存在。如果他们这样做,请让他们登录并继续使用应用程序,如果他们没有,请转到帐户创建流程,他们将被指示添加姓名、电话号码等。
我一辈子都找不到有关如何让用户登录使用 AWS Cognito 的文档。我应该能够传递电子邮件/传递调用并得到回复说用户存在/用户不存在或其他什么!我在这里遗漏了什么吗?
如有任何帮助,我们将不胜感激。我已经搜索了文档..这是我最后的选择。
最佳答案
在当前的 SDK 中,在您的 AWSCognitoIdentityUserPool
上调用 getUser
只会构建内存中的用户对象。要通过网络进行调用,您需要对构造的用户调用 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
})
}
作为引用,我的 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"
}
关于iOS - AWS Cognito - 检查用户是否已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40775841/