[英]How to get AWS Cognito user attributes using AWSMobileClient in iOS?
Question is very simple: I've added user authentication to iOS app using AWS Cognito and AWS Amplify.问题很简单:我使用 AWS Cognito 和 AWS Amplify 向 iOS 应用程序添加了用户身份验证。 I have successfully implemented sign in and sign up, but how to get user attributes such as email, full name or phone number?
我已成功实现登录和注册,但如何获取用户属性,如电子邮件、全名或电话号码?
UPDATE:更新:
For AWSMobileClient ~> 2.12.0, you can fetch user attributes as follows.对于 AWSMobileClient ~> 2.12.0,您可以按如下方式获取用户属性。
AWSMobileClient.default().getUserAttributes { (attributes, error) in
if(error != nil){
print("ERROR: \(error)")
}else{
if let attributesDict = attributes{
print(attributesDict["email"])
print(attributesDict["given_name"])
}
}
}
Per the documentation there are several property helpers for common attributes like username:根据文档,有几个属性助手用于用户名等常见属性:
AWSMobileClient.getInstance().getUsername()
AWSMobileClient.getInstance().isSignedIn()
AWSMobileClient.getInstance().getIdentityId()
You can also get the JWT token and then pull out any user attributes:您还可以获取 JWT 令牌,然后提取任何用户属性:
AWSMobileClient.getInstance().getTokens().getIdToken().getTokenString()
You can use the getUserAttributes with the following API in the latest SDK version 2.8.x
:您可以在最新的 SDK 版本 2.8.x 中通过以下 API 使用
2.8.x
:
public func getUserAttributes(completionHandler: @escaping (([String: String]?, Error?) -> Void))
You can find the source code here:你可以在这里找到源代码:
https://github.com/aws-amplify/aws-sdk-ios/blob/master/AWSAuthSDK/Sources/AWSMobileClient/AWSMobileClientExtensions.swift#L532 https://github.com/aws-amplify/aws-sdk-ios/blob/master/AWSAuthSDK/Sources/AWSMobileClient/AWSMobileClientExtensions.swift#L532
In case you're looking for the email address specifically, and need to do so potentially offline, this would work for you:如果您正在专门寻找电子邮件地址,并且可能需要离线进行,这对您有用:
AWSMobileClient.sharedInstance().getTokens { (tokens, error) in
if let error = error { print(error.localizedDescription) }
if let tokens = tokens {
let email = tokens.idToken?.claims?["email"] as? String
//completionHandler(email)... etc.
}
While AWSMobileClient.sharedInstance().getUsername()
would be convenient, it will return the id of a User Pool user even if the User Pool is set to use email as the username.虽然
AWSMobileClient.sharedInstance().getUsername()
会很方便,但它会返回用户池用户的 ID,即使用户池设置为使用电子邮件作为用户名也是如此。 I consider this a bug, but have yet to report it to AWS.我认为这是一个错误,但尚未向 AWS 报告。
I also research it on android (Kotlin).我还在 android (Kotlin) 上研究它。
// retrieve username
val username = AWSMobileClient.sharedInstance().username
When you sign in with "email" and "password", "username" is "email".当您使用“电子邮件”和“密码”登录时,“用户名”为“电子邮件”。
On the other hand, when the case of iOS (Swift), "username" is really "username" of cognito User Pool, even if you sign in with "email" and "password".另一方面,在 iOS (Swift) 的情况下,“用户名”实际上是 cognito 用户池的“用户名”,即使您使用“电子邮件”和“密码”登录也是如此。
It is so confusing...太混乱了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.