我正在尝试为编写Swift 3的iOS应用程序创建用户登录系统。使用下面的代码,我在用户池中为用户获取了唯一的Cognito Identity ID,但我不知道如何处理此ID。我可以将其链接到用户并获取与该用户关联的属性吗?
代码:
@IBAction func loginPressed(_ sender: Any) {
user = self.pool!.getUser(usernameTextField.text!)
user?.getSession(usernameTextField.text!, password: passwordTextField.text!, validationData: nil).continue({ task in
if let err = task.error { // some sort of error
print("LOGIN FAILED")
print(err)
//print(err.userInfo["message"] as! String)
}
else { //Successful login!
// this gets our token from the User Pool
let ret = task.result! as AWSCognitoIdentityUserSession
let myToken = ret.idToken?.tokenString;
print("Token: ", myToken);
let customcedentialProvider = AWSCustomIdentityProvider(tokens: [AWSCustomIdentityProvider.CognitoTokenKey : myToken!])
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoConstants.COGNITO_REGIONTYPE, identityPoolId: CognitoConstants.COGNITO_IDENTITY_POOL_ID, identityProviderManager: customcedentialProvider)
let configuration = AWSServiceConfiguration(region: CognitoConstants.COGNITO_REGIONTYPE, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
// wipe cached creds
credentialsProvider.clearKeychain()
credentialsProvider.clearCredentials()
// hit it
credentialsProvider.getIdentityId().continue({ (task: AWSTask!) -> AnyObject! in
if (task.error != nil) {
print("Error: ")
} else {
// the task result will contain the identity id
let UserIdentityID = task.result as String? // Im saving user identity id in constant variable called "kUserIdentityID"
print(UserIdentityID)// my identityID
print(credentialsProvider.identityId)// my identityID
}
return nil
})
}
return nil
})
}
输出(身份证号码):
Optional("us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
谢谢!
答案 0 :(得分:1)
Cognito UserPool是一个“身份提供者”,如Facebook,Google等。而Cognito Federated Identity有助于将不同登录之间的映射保持为一个唯一ID,通常称为“Cognito Identity ID”。通常,您在Cognito Federated身份下的数据集内存储和检索数据。并同步多个登录。如果我理解您的问题,您可能希望将userpool“username”存储在上述数据集中,以便您可以检索它并查询用户池
答案 1 :(得分:1)
The self.pool refers to your Cognito User Pool. User pools have at least a username and password, but usually also other attributes, which you can fetch using user.getDetails. A user pool is an IdP (an Identity Provider).
The identityId is a Cognito Identity concept which primarily has the purpose of providing a unique id for one or more identities from IdP's.
Don't worry about being confused. Cognito is very confusing, I found it so confusing that I wrote up a little powerpoint presentation from my notes. Here is a link to a diagram that should help you. Diagram of Cognito
另外,我建议您使用AWS Mobile Hub Helper作为起点。它将下载一个快速的代码示例应用程序。示例代码使用aws-mobilehub-helper-ios包装器,它简化了许多SDK并使其更加合理。下载的代码是Swift2,但在swift 3中运行并不是很难。