Integrating Congnito User Pools with Amazon Cognito Identity

℡╲_俬逩灬. 提交于 2019-12-13 02:31:45

问题


I am using Xcode(Swift). I tried writing code that will sign up, confirm user and sign in user to the user pool. Then on successful sign in, I want it to be linked with Amazon Cognito Identity. Basically, I want to have a as many Identity Id as many users in User Pool.

So far I am able to sign up, confirm user and sign in (using explicit login). On Success of Sign In, I am trying to link this user to Cognito Identity Pool so that a unique Id can be generated for this user.

Currently, If I SignIn to the application as a different user it is assigned the same Identity Id as the previous user. In other words, Irrespective of how many users are there in my User Pool, On the AWS Cogntio Federated identity pool side, I have only one Identity Id. Ideally, It should have created separate Identity Id for different Users.

Below is the code in App delegate within didFinishLaunchingWithOptions function.

let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: nil)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = serviceConfiguration
let configurationUserPool = AWSCognitoIdentityUserPoolConfiguration.init(clientId: "####", clientSecret: "####", poolId: "####")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: configurationUserPool, forKey: "testPool")
self.userPool = AWSCognitoIdentityUserPool(forKey: "testPool")

Below is the code in Sign View controller.

@IBAction func signIn(sender: AnyObject) {

    let user = self.userPool.getUser(userName.text!)

    user.getSession(userName.text!, password: password.text!, validationData: nil, scopes: nil).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {
        (task:AWSTask!) -> AnyObject! in

        if task.error != nil {
            print(task.error)
        } else {
            print("Successful Login")

            let cp = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "######", identityProviderManager:self.userPool)
            let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: cp)

            AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration                
            cp.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in

                if (task.error != nil) {
                    print("Error: ")

                } else {
                    // the task result will contain the identity id
                    print("Success with id")
                    print(task.result)
                }
                return nil
            }

            dispatch_async(dispatch_get_main_queue()){
               // do stuff here ...
            }
        }
        return nil
    })
}

回答1:


Finally, I was able to rectify this issue. The problem was that each time I was SignIn a new user, I was resetting the credentials provider. When I moved it to the App Delegate, It started working the way it should.

This should not be in signIn function, rather it should be App Delegate.

let cp = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "######", identityProviderManager:self.userPool)
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: cp)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration                

Correct way.

let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: nil)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = serviceConfiguration
let configurationUserPool = AWSCognitoIdentityUserPoolConfiguration.init(clientId: "####", clientSecret: "####", poolId: "####")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: configurationUserPool, forKey: "testPool")
self.userPool = AWSCognitoIdentityUserPool(forKey: "testPool")
self.cp = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "######", identityProviderManager:self.userPool)
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: self.cp)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration                


来源:https://stackoverflow.com/questions/37491909/integrating-congnito-user-pools-with-amazon-cognito-identity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!