简体   繁体   English

如何在 iOS 中使用 AWSMobileClient 获取 AWS Cognito 用户属性?

[英]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.

相关问题 Nest.js 与 AWS Cognito,如何访问用户属性 - Nest.js with AWS Cognito, How to get access to the user attributes 如何在aws cognito中更新用户的自定义属性 - how to update custom attributes of user in aws cognito 无法使用来自 AWS Cognito 的配置文件/openid 范围获取用户属性 - Unable to get user attributes using profile/openid scopes from AWS Cognito 如何获取 Flutter 中的 AWS Cognito 用户组? - How to get the AWS Cognito User group in Flutter? 如何使用 AWS Cognito 添加额外的注册属性? - How to add additional signup attributes using AWS Cognito? 如何使用 Cognito 身份 ID 获取用户属性(用户名、email 等) - How to get user attributes (username, email, etc.) using cognito identity id 如何从 AWS Cognito 上的托管 UI 获取用户池令牌 - How to get the User pool token from Hosted UI on AWS Cognito AWS + 无服务器 - 如何获取 Cognito 用户池生成的密钥 - AWS + Serverless - how to get at the secret key generated by cognito user pool 在不让用户登录的情况下更新 AWS Cognito 用户的自定义属性 - Update AWS Cognito user's custom attributes without logging user in aws cognito 用户获取 id 令牌 android - aws cognito user get id token android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM