Search code examples
iosswiftamazon-web-servicesamazon-cognitotwitter-digits

AWS Cognito does not create Twitter/Digit login when called from Swift


So I'm trying to login user to my app using their phone number. For this I've integraed Digit from Twitter's Fabric Kit as well as AWS Cognito. I'm able to authenticate users using digit and successfully getting the session object and can extract the userid, phone number, authToken and the authTokenSecret. However I'm unable to link the digit authenticated user with cognito. Essentially credentialsProvider.logins = ["www.digits.com": value] (even if it executes) has no impact at all in the configured cognito identity pool.

Here's my viewcontroller.swift

import UIKit
import DigitsKit

let credentialsProvider = AWSCognitoCredentialsProvider(
regionType: AWSRegionType.USEast1, identityPoolId: "IDENTITY_POOL_ID")

let defaultServiceConfiguration = AWSServiceConfiguration(
region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)


class ViewController: UIViewController {

@IBAction func login(sender: AnyObject) {
    let digits = Digits.sharedInstance()
    digits.authenticateWithCompletion { (session, error) in
        if (session != nil) {
            println("session user id is: " + session.userID)
            println("session mobile no is: " + session.phoneNumber)
            println("session token is: " + session.authToken)
            println("session authtoken secret is: " + session.authToken)
            var value = session.authToken + ";" + session.authTokenSecret

            //THIS PART DOES NOT SET DIGIT LOGIN WITH COGNITO
            credentialsProvider.logins = ["www.digits.com": value]
        }
        // Inspect session/error objects
    }


}

@IBAction func logout(sender: AnyObject) {
        }
override func viewDidLoad() {
    super.viewDidLoad()

    AWSServiceManager.defaultServiceManager().
defaultServiceConfiguration = defaultServiceConfiguration

    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

I've replaced the correct identity pool id and have copied the consumer key and secret to amazon cognito pool. I've disabled unauthenticated ids. What am I doing wrong?


Solution

  • The AWSCognitoCredentialsProvider is lazily loaded. Simply setting the logins dictionary is not enough to cause a login to appear in the console. Please try one of the following:

    1. You can use the credentials provider to access any other service, for instance using the Amazon Cognito Sync service to store information for the identity and synchronize to the cloud.
    2. You can also force the credentials provider to make a call to the Amazon Cognito Identity service. If you do not have a cached identity id, the getIdentitId method will make a call to the service to establish your identityId.
    3. If you already have an identity cached, you can use the refresh method to force re-authentication with the contents of the logins dictionary.