简体   繁体   English

注册后无法使用iOS Cognito UserPool SDK改变用户属性

[英]Unable to mutate user attributes using iOS Cognito UserPool SDK after signup

I am trying to change a user's attribute (ie family name) after they have signed up. 我正在尝试在注册后更改用户的属性(即姓氏)。 This is an attribute that has been selected in the cognito user pool and was filled out when they initially signed up. 这是在cognito用户池中选择的属性,并在最初注册时填写。 I have only setup a user pool and not an identity pool. 我只设置了一个用户池,而不是一个身份池。

For that, I am using the method adminUpdateUserAttributes in the AWSCognitoIdentityProvider , using the iOS SDK but it gives me the following error: 对于这一点,我使用的方法adminUpdateUserAttributesAWSCognitoIdentityProvider ,使用iOS的SDK,但它给了我下面的错误:

Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)" UserInfo={__type=MissingAuthenticationTokenException, message=Missing Authentication Token}

Here is my setup in the AppDelegate: 这是我在AppDelegate中的设置:

    let clientId:String = self.cognitoConfig!.getClientId()
    let poolId:String = self.cognitoConfig!.getPoolId()
    let clientSecret:String = self.cognitoConfig!.getClientSecret()
    let region:AWSRegionType = self.cognitoConfig!.getRegion()



    let serviceConfiguration:AWSServiceConfiguration = AWSServiceConfiguration(region: region, credentialsProvider: nil)
    let cognitoConfiguration:AWSCognitoIdentityUserPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: clientId, clientSecret: clientSecret, poolId: poolId)
    AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: cognitoConfiguration, forKey: userPoolID)
    let pool:AWSCognitoIdentityUserPool = AppDelegate.defaultUserPool()
    pool.delegate = self

    AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration

Here is my code for actually trying to change the attribute 这是我实际尝试更改属性的代码

    let identityProvider = AWSCognitoIdentityProvider.default()


    let requestAttributeChange = AWSCognitoIdentityProviderAdminUpdateUserAttributesRequest()
    requestAttributeChange?.username = user?.username
    requestAttributeChange?.userPoolId = AppDelegate.defaultUserPool().userPoolConfiguration.poolId

    let attribute = AWSCognitoIdentityProviderAttributeType.init()
    attribute?.name = "given_name"
    attribute?.value = "TEST"

    if let att = attribute {

        print("Change attribute")
        requestAttributeChange?.userAttributes = [att]

        identityProvider.adminUpdateUserAttributes(requestAttributeChange!).continueWith(block: { (res) -> Any? in
            print(res.error)
        })
    }

Do I need to setup a separate identity pool too? 我是否还需要设置单独的标识池? I am also not sure about the type of data/keys that i will need to store in addition to get more access? 除了获得更多访问权限之外,我还不确定我需要存储的数据/密钥类型? As I am trying to avoid storing any sensitive data on the actual device. 因为我试图避免在实际设备上存储任何敏感数据。

So, my approach will not work, as it requires me to also create an identity pool as well. 所以,我的方法不起作用,因为它也要求我也创建一个身份池。 If you already have an identity pool, then the above approach might work but its a bit dirty/unsecure-ish as you are forcing your way as an admin to do something very simple. 如果你已经有了一个身份池,那么上面的方法可能会有效,但它有点脏/不安全,因为你强迫管理员做一些非常简单的事情。

I found a better solution thanks to this issue and this resource . 由于这个问题这个资源,我找到了一个更好的解决方案。

Here is the same answer in swift 4 syntax. 这是swift 4语法中的相同答案。

let attr = AWSCognitoIdentityUserAttributeType.init(name: "given_name", value: "TEST")

user?.update([attr]).continueOnSuccessWith(block: { (response) -> Any? in
    // Was successful, do any changes, UI changes must be done on the main thread.
    return nil
 })

You can also update custom attributes this way as well. 您也可以通过这种方式更新自定义属性 Just make sure that when you add a custom attribute via the aws panel, you go into your AWS Cognito UserPool. 只需确保在通过aws面板添加自定义属性时,您将进入AWS Cognito UserPool。 Go to General Settings -> App Clients -> Click on "Show Details" -> Click on "Set attribute read and write permissions" and specify appropriate read and write permissions for your newly created custom attributes. 转到常规设置 - >应用程序客户端 - >单击“显示详细信息” - >单击“设置属性读取和写入权限”,并为新创建的自定义属性指定适当的读写权限。

When updating custom attributes, you only need to include the custom tag before the attribute name. 更新自定义属性时,只需在属性名称前包含custom标记。 So, for example, an attribute called school would be created as follows. 因此,例如,将创建名为school的属性,如下所示。

let attr = AWSCognitoIdentityUserAttributeType.init(name: "custom:school", value: "Junior High")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS iOS Cognito-如何在Userpool中检查用户? - AWS iOS Cognito - How To Check For a User in a Userpool? 如何在 iOS 中使用 AWSMobileClient 获取 AWS Cognito 用户属性? - How to get AWS Cognito user attributes using AWSMobileClient in iOS? 如何将用户添加到AWS Cognito用户池组中? - How to add a user into a AWS Cognito userpool group? 使用iOS SDK使用Facebook登录和注册 - login vs signup with facebook using iOS SDK 使用iOS SDK Cognito Lambda和DynamoDB为AWS iOS创建用户/开发人员定义的登录 - Creating a user/developer defined login for AWS iOS using iOS SDK Cognito Lambda and DynamoDB 实施问题 使用 iOS 的 Lambda 触发器迁移 Cognito UserPool - Implementation Issue Migrate Cognito UserPool with Lambda Trigger for iOS 使用AWS开发工具包iOS版本2.6.1登录后无法访问用户详细信息userName,imageURL - Unable to access user details userName , imageURL after login using AWS SDK iOS version 2.6.1 如何使用iOS SDK使用Facebook身份验证使用DynamoDB和Cognito存储用户信息 - How to store user information with DynamoDB and Cognito using Facebook authentication with iOS SDK Quickblox iOS SDK:注册后无法发送通知 - Quickblox iOS SDK: cannot send notification after signup 从未认证切换到经过开发人员认证的Cognito用户-AWS iOS SDK - Switch from unauth to developer authenticated cognito user - AWS iOS SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM